Ulli
Ulli

Reputation: 21

get paragraph with awk, and start-of-line regexp

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?

Upvotes: 2

Views: 257

Answers (1)

Ed Morton
Ed Morton

Reputation: 203985

^ 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

Upvotes: 4

Related Questions