How to filter out lines?

  • Follow


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?
0
Reply postoronnimv77 (7) 2/24/2008 7:38:03 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?

[GNU sed]

stdout_of_some_prog | sed "/marker1/{N;/marker2/d}"

-- 
   Best regards  |  Be nice to America or they'll bring democracy to
   Cyrus         |  your country.
0
Reply Cyrus 2/24/2008 8:00:34 PM


Viatly schreef:
> 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?

following is untested, but should almost work:

awk '/marker1/{ mark=1 }
      /marker2/{ if (mark==1) { mark=mark+1 } else { mark=0 }}
      { if (mark==2) { do something because 'marker1' was found and 
'marker2' on next line.... }' file

-- 
Luuk
0
Reply Luuk 2/24/2008 8:16:28 PM


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.

0
Reply Ed 2/24/2008 9:20:35 PM

On Feb 24, 9:00 pm, Cyrus Kriticos <cyrus.kriti...@googlemail.com>
wrote:
> 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?
>
> [GNU sed]
>
> stdout_of_some_prog | sed "/marker1/{N;/marker2/d}"
>
> --
>    Best regards  |  Be nice to America or they'll bring democracy to
>    Cyrus         |  your country.

Thanks, it works just fine after I after removed curly brackets from
your code.
0
Reply Viatly 2/24/2008 9:37:42 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...

$ echo "some text..
more text...marker1...moretext
more text...marker2...moretext
more text..
more text...marker1...moretext
more text..
more text...marker2...moretext
more text..." | \
     perl -ne'/marker1/ and $_ .= <> and /marker2/ and next; print'
some text..
more text..
more text...marker1...moretext
more text..
more text...marker2...moretext
more text...




John
-- 
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall
0
Reply John 2/24/2008 11:11:32 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?

ruby -e'puts gets(nil).gsub(/^.*marker1.*\n.*marker2.*\n/,"")'
0
Reply William 2/25/2008 3:21:08 AM

6 Replies
241 Views

(page loaded in 0.148 seconds)

Similiar Articles:













7/29/2012 10:25:37 PM


Reply: