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 回答