2

I use awk to get paragraphs from a textfile, like so:

awk -v RS='' -v ORS='\n\n' '/pattern/' ./textfile

Say I have the following textfile:

aaa bbb ccc
aaa bbb ccc
aaa bbb ccc

aaa ccc
bbb aaa ccc
bbb aaa ccc

ccc bbb aaa
ccc bbb aaa
ccc bbb aaa

Now I only want the paragraph with one of the (original) lines starting with "bbb" (hence the second paragraph). However - using regexp ^ will not work anymore, (I presume) because of the RS='' line; awk now only matches to the begin of the paragraph.

Is there another way?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Ulli
  • 21
  • 2

1 Answers1

4

^ means start-of-string. You want start-of-line which is (^|\n), e.g.:

$ awk -v RS='' -v ORS='\n\n' '/(^|\n)bbb/' file
aaa ccc
bbb aaa ccc
bbb aaa ccc
Ed Morton
  • 188,023
  • 17
  • 78
  • 185