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

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

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

问题

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

  1. -----BEGIN CERTIFICATE-----
  2. this is sentence1
  3. this is sentence2
  4. -----END CERTIFICATE-----
  5. -----BEGIN CERTIFICATE-----
  6. this is sentence3
  7. this is sentence4
  8. -----END CERTIFICATE-----
  9. -----BEGIN CERTIFICATE-----
  10. this is sentence5
  11. this is sentence6
  12. -----END CERTIFICATE-----
  13. -----BEGIN CERTIFICATE-----
  14. this is sentence7
  15. this is sentence8
  16. -----END CERTIFICATE-----

I want the output to look like:

  1. -----BEGIN CERTIFICATE-----
  2. this is sentence7
  3. this is sentence8
  4. -----END CERTIFICATE-----
  5. -----BEGIN CERTIFICATE-----
  6. this is sentence5
  7. this is sentence6
  8. -----END CERTIFICATE-----
  9. -----BEGIN CERTIFICATE-----
  10. this is sentence3
  11. this is sentence4
  12. -----END CERTIFICATE-----
  13. -----BEGIN CERTIFICATE-----
  14. this is sentence1
  15. this is sentence2
  16. -----END CERTIFICATE-----

I have tried the following:

  1. tac -s'-----BEGIN CERTIFICATE-----' mycert.cer | awk '/-----BEGIN CERTIFICATE-----/, /-----END CERTIFICATE-----/'

but this command gave the following incorrect output:

  1. -----BEGIN CERTIFICATE-----
  2. this is sentence3
  3. this is sentence4
  4. -----END CERTIFICATE-----
  5. -----BEGIN CERTIFICATE-----
  6. this is sentence1
  7. this is sentence2
  8. -----END CERTIFICATE-----
  9. -----BEGIN CERTIFICATE----------BEGIN CERTIFICATE-----

How do I fix the command so that I get the desired output?

答案1

得分: 3

使用任何awk加上tac:

  1. $ tac mycert.cer | awk '
  2. { cert = $0 ORS cert }
  3. /BEGIN CERT/ { printf "%s", cert; cert="" }
  4. '
  5. -----BEGIN CERTIFICATE-----
  6. 这是句子7
  7. 这是句子8
  8. -----END CERTIFICATE-----
  9. -----BEGIN CERTIFICATE-----
  10. 这是句子5
  11. 这是句子6
  12. -----END CERTIFICATE-----
  13. -----BEGIN CERTIFICATE-----
  14. 这是句子3
  15. 这是句子4
  16. -----END CERTIFICATE-----
  17. -----BEGIN CERTIFICATE-----
  18. 这是句子1
  19. 这是句子2
  20. -----END CERTIFICATE-----

或者只使用awk:

  1. $ awk '
  2. { cert = cert $0 ORS }
  3. /END CERT/ { certs[++numCerts] = cert; cert="" }
  4. END { for (i=numCerts; i>=1; i--) printf "%s", certs[i] }
  5. ' mycert.cer
  6. -----BEGIN CERTIFICATE-----
  7. 这是句子7
  8. 这是句子8
  9. -----END CERTIFICATE-----
  10. -----BEGIN CERTIFICATE-----
  11. 这是句子5
  12. 这是句子6
  13. -----END CERTIFICATE-----
  14. -----BEGIN CERTIFICATE-----
  15. 这是句子3
  16. 这是句子4
  17. -----END CERTIFICATE-----
  18. -----BEGIN CERTIFICATE-----
  19. 这是句子1
  20. 这是句子2
  21. -----END CERTIFICATE-----
英文:

Using any awk plus tac:

  1. $ tac mysert.cer | awk '
  2. { cert = $0 ORS cert }
  3. /BEGIN CERT/ { printf "%s", cert; cert="" }
  4. '
  5. -----BEGIN CERTIFICATE-----
  6. this is sentence7
  7. this is sentence8
  8. -----END CERTIFICATE-----
  9. -----BEGIN CERTIFICATE-----
  10. this is sentence5
  11. this is sentence6
  12. -----END CERTIFICATE-----
  13. -----BEGIN CERTIFICATE-----
  14. this is sentence3
  15. this is sentence4
  16. -----END CERTIFICATE-----
  17. -----BEGIN CERTIFICATE-----
  18. this is sentence1
  19. this is sentence2
  20. -----END CERTIFICATE-----

or just awk:

  1. $ awk '
  2. { cert = cert $0 ORS }
  3. /END CERT/ { certs[++numCerts] = cert; cert="" }
  4. END { for (i=numCerts; i>=1; i--) printf "%s", certs[i] }
  5. ' mycert.cer
  6. -----BEGIN CERTIFICATE-----
  7. this is sentence7
  8. this is sentence8
  9. -----END CERTIFICATE-----
  10. -----BEGIN CERTIFICATE-----
  11. this is sentence5
  12. this is sentence6
  13. -----END CERTIFICATE-----
  14. -----BEGIN CERTIFICATE-----
  15. this is sentence3
  16. this is sentence4
  17. -----END CERTIFICATE-----
  18. -----BEGIN CERTIFICATE-----
  19. this is sentence1
  20. this is sentence2
  21. -----END CERTIFICATE-----

答案2

得分: 1

在awk中:

  1. awk 'BEGIN{RS="";ORS=FS="-----END CERTIFICATE-----\n"}{while(--NF)print $NF}' 文件名
  2. # 或稍微长一点的版本
  3. awk -v RS="" -v FS="-----END CERTIFICATE-----\n" '{while(--NF)printf "%s",$NF FS}' 文件名
  4. # 如果你找到一个不允许更改NF的awk(我还没有找到过)
  5. # 把操作改成 for(i=NF;--i;)print $i 或 printf "%s",$i FS

在perl中:

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

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

英文:

In awk:

  1. awk 'BEGIN{RS="";ORS=FS="-----END CERTIFICATE-----\n"}{while(--NF)print $NF}' file
  2. # or slightly longer
  3. awk -v RS="" -v FS="-----END CERTIFICATE-----\n" '{while(--NF)printf "%s",$NF FS}' file
  4. # or you ever find an awk that doesn't allow changing NF (I haven't)
  5. # change action to for(i=NF;--i;)print $i resp printf "%s%,$i FS

In perl:

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

确定