我需要在文件中找到 pgp 加密消息。它们以 -----BEGIN PGP MESSAGE-----
开始,以 -----END PGP MESSAGE-----
结束。
到目前为止我有这个:
$ tail -200 somefile | awk '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/'
它正在查找所有出现的事件,但我只想要最后一个。
最佳答案
你可以使用 sed:
tail -200 somefile | sed -n '
# only consider lines between BEGIN and END
/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/ {
# if the beginning line, clear the hold space
/-----BEGIN PGP MESSAGE-----/{x;d}
# add the line to the hold space
H
};
# print the hold space at the end
${x;p}'
这个sed注释(注释是为了解释,在实际命令中不需要),“BEGIN”和“END”之间的任何行都将添加到保留空间,并在每个“BEGIN”时清除保留空间"然后打印在最后。
编辑:
为了完整起见,这里是没有注释且在一行中的版本(与上面相同)
tail -200 somefile | sed -n '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/{/-----BEGIN PGP MESSAGE-----/{x;d};H};${x;p}'
关于linux - 如何在文本中搜索多行模式并获得最后一次出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19918642/