malmo
malmo

Reputation: 524

print a paragraph that contains more than 2 patterns using awk or sed

I want to print only the paragraphs that begin with "select" and end with ";", but should contain "IN cccc" for example if I have the following code

select aaaa
bbbb
IN cccc
;
select dddd
eeeee
IN ffff
;
select rrrr
eeeee
IN cccc
;

the result would be like

select aaaa
bbbb
IN cccc
;
select rrrr
eeeee
IN cccc
;

so here's the awk I used awk '/^select*/,/^;/{if ($0 ~ /^IN cccc/) ; print}' inputfile but I get as a result the entire content of my file

Upvotes: 1

Views: 198

Answers (1)

sat
sat

Reputation: 14949

You can try this awk:

awk 'BEGIN{ORS=RS=";"} /^[[:space:]]*select.*IN cccc/' file

Here,

  • ORS - Output Record Separator
  • RS - Input Record Separator

Upvotes: 4

Related Questions