[英]How do you reverse the certificate blocks given a CER file using a linux script?
How do you reverse the certificate blocks given a CER file using a linux script (ie awk, sed, etc..)?如何使用 linux 脚本(即 awk、sed 等)反转给定 CER 文件的证书块?
So suppose I have a file called mycert.cer that looks like:所以假设我有一个名为 mycert.cer 的文件,它看起来像:
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence5
this is sentence6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence7
this is sentence8
-----END CERTIFICATE-----
I want the output to look like:我希望 output 看起来像:
-----BEGIN CERTIFICATE-----
this is sentence7
this is sentence8
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence5
this is sentence6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
I have tried the following:我尝试了以下方法:
tac -s'-----BEGIN CERTIFICATE-----' mycert.cer | awk '/-----BEGIN CERTIFICATE-----/, /-----END CERTIFICATE-----/'
but this command gave the following incorrect output:但是此命令给出了以下错误的 output:
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE----------BEGIN CERTIFICATE-----
How do I fix the command so that I get the desired output?如何修复命令以便获得所需的 output?
Using any awk plus tac:使用任何 awk 加上 tac:
$ tac mysert.cer | awk '
{ cert = $0 ORS cert }
/BEGIN CERT/ { printf "%s", cert; cert="" }
'
-----BEGIN CERTIFICATE-----
this is sentence7
this is sentence8
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence5
this is sentence6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
or just awk:或者只是 awk:
$ awk '
{ cert = cert $0 ORS }
/END CERT/ { certs[++numCerts] = cert; cert="" }
END { for (i=numCerts; i>=1; i--) printf "%s", certs[i] }
' mycert.cer
-----BEGIN CERTIFICATE-----
this is sentence7
this is sentence8
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence5
this is sentence6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
In awk:在 awk:
awk 'BEGIN{RS="\1";ORS=FS="-----END CERTIFICATE-----\n"}{while(--NF)print $NF}' file
# or slightly longer
awk -v RS="\1" -v FS="-----END CERTIFICATE-----\n" '{while(--NF)printf "%s",$NF FS}' file
# or you ever find an awk that doesn't allow changing NF (I haven't)
# change action to for(i=NF;--i;)print $i resp printf "%s%,$i FS
In perl:在 perl:
perl -n0777e 'map{print}reverse split $\="-----END CERTIFICATE-----\n"' file
Both of these treat the whole file as one 'record' and if your awk is not GNU (fairly rare nowadays but not unknown) it might fail for a too-large file.这两个都将整个文件视为一个“记录”,如果您的 awk 不是 GNU(如今相当罕见但并非未知),它可能会因文件过大而失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.