Home > Archive > AWK > February 2008 > How to filter out lines?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to filter out lines?
|
|
| Viatly 2008-02-24, 6:59 pm |
| 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?
| |
|
| 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
| |
| Ed Morton 2008-02-24, 6:59 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.
| |
| John W. Krahn 2008-02-24, 6:59 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
| |
| William James 2008-02-24, 10:11 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/,"")'
|
|
|
|
|