如何使用Linux脚本反转给定CER文件的证书块?

huangapple go评论50阅读模式
英文:

How do you reverse the certificate blocks given a CER file using a linux script?

问题

tac -s '-----BEGIN CERTIFICATE-----' mycert.cer | awk '/-----BEGIN CERTIFICATE-----/,/-----END CERTIFICATE-----/' | tac
英文:

How do you reverse the certificate blocks given a CER file using a linux script (i.e. awk, sed, etc..) ?

So suppose I have a file called mycert.cer that looks like:

-----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:

-----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:

-----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?

答案1

得分: 3

使用任何awk加上tac:

$ tac mycert.cer | awk '
    { cert = $0 ORS cert }
    /BEGIN CERT/ { printf "%s", cert; cert="" }
'
-----BEGIN CERTIFICATE-----
这是句子7
这是句子8
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子5
这是句子6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子3
这是句子4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子1
这是句子2
-----END CERTIFICATE-----

或者只使用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-----
这是句子7
这是句子8
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子5
这是句子6
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子3
这是句子4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
这是句子1
这是句子2
-----END CERTIFICATE-----
英文:

Using any awk plus 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 '
    { 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-----

答案2

得分: 1

在awk中:

awk 'BEGIN{RS="";ORS=FS="-----END CERTIFICATE-----\n"}{while(--NF)print $NF}' 文件名
# 或稍微长一点的版本
awk -v RS="" -v FS="-----END CERTIFICATE-----\n" '{while(--NF)printf "%s",$NF FS}' 文件名

# 如果你找到一个不允许更改NF的awk(我还没有找到过)
# 把操作改成 for(i=NF;--i;)print $i 或 printf "%s",$i FS

在perl中:

perl -n0777e 'map{print}reverse split $/="-----END CERTIFICATE-----\n"' 文件名

这两种方法都将整个文件视为一个"record",如果你的awk不是GNU版本(现在相当少见但不无可能),它在处理过大的文件时可能会失败。

英文:

In awk:

awk 'BEGIN{RS="";ORS=FS="-----END CERTIFICATE-----\n"}{while(--NF)print $NF}' file
# or slightly longer
awk -v RS="" -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 -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.

huangapple
  • 本文由 发表于 2023年6月8日 03:31:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定