简体   繁体   English

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

[英]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.

相关问题 如何使用 powershell 将数字证书(.cer 文件)的详细信息导出到 csv 文件? - How to export the details of a digital certificate (.cer file) to a csv file using powershell? 在Linux bash中,反向文件行顺序为,但每3行块一次 - In linux bash reverse file lines order but for blocks each 3 lines 您如何将stdout作为php脚本中的参数传递给linux终端? - How do you pass the stdout to a linux terminal as an argument in a php script? Linux:查找给定“原始”文件的所有符号链接? (反向“阅读链接”) - Linux: Find all symlinks of a given 'original' file? (reverse 'readlink') 如何使用Mailx将脚本文件发送给Linux上Host PC上的另一个用户 - How do i send a script file using mailx to another user on the Host pc on Linux 在 linux 上,使用 bash 脚本如何重命名 Excel 文件以在现有文件名末尾包含行数 - On linux , using a bash script how do I rename an Excel file to include the row count at the end of the existing filename 如何使用bash在文件更改后设置脚本RELOAD / RESTART? - How do you setup script RELOAD/RESTART upon file changes using bash? 如何逆转文本块的顺序 - How to reverse order of blocks of text Linux:如何使用脚本委派奇异的命令行参数? - Linux: How do I delegate exotic commandline arguments using a script? 如何在Linux中使用Bash脚本对这些值进行排序? - How do I sort these values using Bash Script in Linux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM