ファイル内の pgp 暗号化メッセージを見つける必要があります。で始まり、-----BEGIN PGP MESSAGE-----
で終わり-----END PGP MESSAGE-----
ます。
これまでのところ、私はこれを持っています:
$ tail -200 somefile | awk '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/'
すべてのオカレンスを見つけていますが、最後の 1 つだけが必要です。
ファイル内の pgp 暗号化メッセージを見つける必要があります。で始まり、-----BEGIN PGP MESSAGE-----
で終わり-----END PGP MESSAGE-----
ます。
これまでのところ、私はこれを持っています:
$ tail -200 somefile | awk '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/'
すべてのオカレンスを見つけていますが、最後の 1 つだけが必要です。
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」ごとにクリアされ、次に最後に印刷。
編集:
完全を期すために、ここにコメントなしの 1 行のバージョンを示します (上記と同じことを行います)。
tail -200 somefile | sed -n '/-----BEGIN PGP MESSAGE-----/,/-----END PGP MESSAGE-----/{/-----BEGIN PGP MESSAGE-----/{x;d};H};${x;p}'
BEGIN {
beginmsg = "-----BEGIN PGP MESSAGE-----"
endmsg = "-----END PGP MESSAGE-----"
}
$0 ~ beginmsg {
block = ""
}
beginmsg,endmsg {
block = block $0 ORS
}
END {
printf "%s", block
}