如何使用Linux脚本(即awk、sed等)?
假设我有一个名为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-----
我希望输出看起来像这样:
-----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-----
我尝试了以下方法:
tac -s'-----BEGIN CERTIFICATE-----' mycert.cer | awk '/-----BEGIN CERTIFICATE-----/, /-----END CERTIFICATE-----/'
但此命令给出以下错误输出:
-----BEGIN CERTIFICATE-----
this is sentence3
this is sentence4
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
this is sentence1
this is sentence2
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE----------BEGIN CERTIFICATE-----
我该如何修改命令以获得所需的输出?
2条答案
按热度按时间ux6nzvsh1#
使用任意awk加tac:
或者只是awk:
kuhbmx9i2#
在awk中:
在perl中:
这两种方法都将整个文件视为一个“记录”,如果你的awk不是GNU(现在很少见,但并非未知),它可能会因为文件太大而失败。