views:

232

answers:

1

Hi

I have managed to put text in a file by separating them by blank lines. I am trying to keep only those paragraphs that have a particular string. Though the Sed FAQ mentions a solution it does not work (see examples below)

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;'

Can you please tell me why it is not working. Also if you know a solution with a unix tool or otherwise, please let me know.

Regards,

Suhaas

A: 

for file parsing, use gawk instead. use sed only for simple substitution.

awk -vRS= '/AAA/' file

output:

$ more file
text
text
BBB
text

text
text
AAA
text

text
text
CCC
text

$ awk -vRS= '/AAA/' file
text
text
AAA
text
ghostdog74