Tuesday, September 16, 2014

Print paragraph between semicolons containing a pattern

Below two awk snippets can do this for you:

  • awk 'function dothis(){ if(v && s==2){ print p} if(s==2)s=v=p="" }{ dothis(); s += /;/ ; v += /XXXX/; s ? p = length(p) ? p ORS $0 : $0 : "" }END{dothis()}' file
     

  • awk '($0 ~ /;/){set++;} set{v=v?v ORS $0:$0} (v ~ /XXXX/){value=1} {if(set>1 && value==1){print v;v="";value="";set=""}}'  filename 

No comments:

 

Blogger news

Blogroll