On 2/24/2008 1:38 PM, Viatly wrote:
> Given a stdout of some prog, I have to filter out 2-line blocks of
> text, where the first line contains "marker1" and the second line
> contains "marker2".
> Thus, for the following output
>
> some text..
> more text...marker1...moretext
> more text...marker2...moretext
> more text..
> more text...marker1...moretext
> more text..
> more text...marker2...moretext
> more text...
>
> after applying the filter I shell get
>
> some text..
> more text..
> more text...marker1...moretext
> more text..
> more text...marker2...moretext
> more text...
>
> For one-line case grep whould be just sufficient:
>
> ./prog | grep -v marker1
>
> however for multiline patterns I think grep is not a right choice. How
> it can be done with awk/sed?
awk '
/marker1/ { printf "%s",s; s=$0 ORS; next}
/marker2/ && s { s=""; next }
{ printf "%s%s\n",s,$0; s="" }
END { printf "%s",s }' file
Ed.


|