0

テキストを空白行で区切ってファイルに入れることができました。特定の文字列を持つ段落のみを保持しようとしています。Sed FAQ には解決策が記載されていますが、機能しません (以下の例を参照)。

http://www.catonmat.net/blog/sed-one-liners-explained-part-two/

58. Print a paragraph that contains “AAA”. (Paragraphs are separated by blank lines).
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

http://www.linuxhowtos.org/System/sedoneliner.htm?ref=news.rdf

# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

うまくいかない理由を教えてください。また、UNIX ツールなどを使用した解決策をご存じでしたら、お知らせください。

4

1 に答える 1

0

ファイルの解析には、代わりに gawk を使用してください。sed は単純な置換にのみ使用してください。

awk -vRS= '/AAA/' file

出力:

$ more file
text
text
BBB
text

text
text
AAA
text

text
text
CCC
text

$ awk -vRS= '/AAA/' file
text
text
AAA
text
于 2009-11-09T01:37:58.630 に答える