Grep/awk/sed for multiple/groups of lines in a file???

Grep/awk/sed for multiple/groups of lines in a file???

Post by Stephen Cop » Fri, 17 Jan 2003 09:09:54



I'm having trouble searching for a specfic word and then returning the found
line and the following 3 lines after that.

For example:

Quote:>cat filename

i am testing this script
test
test
test
this is not a test
ntest
ntest
ntest
i am testing this script again
test
test
test

Quote:>awk '/testing/ || /again/{print $0; x=NR+1; y=NR+1; z=NR+1; print x; print

y; print z}' filename
//this just returns the found line and the numbers of the following three
lines

Quote:>sed '/\<testing\>/{;N;N;N;p;}' filename

//this seems to work if there is an embedded return, but how do you AND and
OR with sed

Quote:>egrep ' testing | again ' filename

//this finds and prints the lines but I can't print the next three lines
after the found ones

Any help is much appriciated.  Thanks.

-Steve

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by SCoTT SMeDLe » Fri, 17 Jan 2003 09:36:37


Quote:>>egrep ' testing | again ' filename
> //this finds and prints the lines but I can't print the next three lines
> after the found ones

egrep -A3 ' testing | again ' filename

SCoTT. :)

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Stephen Cop » Fri, 17 Jan 2003 09:58:20


Thanks Scott,

I tried this and -A is not recognized.  Here is what I get:

csh: egrep -A3 'foo' out.txt
egrep: illegal option -- A
egrep: illegal option -- 3
usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ] ...

Should I be doing this in any particular shell.  Thanks

-Steve


Quote:> >>egrep ' testing | again ' filename
> > //this finds and prints the lines but I can't print the next three lines
> > after the found ones

> egrep -A3 ' testing | again ' filename

> SCoTT. :)

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by SCoTT SMeDLe » Fri, 17 Jan 2003 11:24:29


Quote:> I tried this and -A is not recognized.  Here is what I get:  csh: egrep
> -A3 'foo' out.txt
> egrep: illegal option -- A
> egrep: illegal option -- 3
> usage: egrep [ -bchilnsv ] [ -e exp ] [ -f file ] [ strings ] [ file ]
> ...  Should I be doing this in any particular shell.  Thanks  -Steve

Mmmm, the problem is the egrep utility. ie. Linux has a decent version of
egrep (that supports -A, -B, etc) but solaris does not.

This isn't pretty, but it works:

perl -ne 'BEGIN {$n = -1}; /foo/ && ($n = 3); print if ($n >= 0); $n--;' filename

SCoTT. :)

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Dr. Yuan Li » Fri, 17 Jan 2003 16:49:51



> I'm having trouble searching for a specfic word and then returning the found
> line and the following 3 lines after that.

> For example:

>>cat filename

> i am testing this script
> test
> test
> test
> this is not a test
> ntest
> ntest
> ntest
> i am testing this script again
> test
> test
> test
[snip]

>>sed '/\<testing\>/{;N;N;N;p;}' filename

> //this seems to work if there is an embedded return, but how do you AND and
> OR with sed

Can you explain what do you mean by AND/OR?  What exactly do you want to
achieve?  I wouldn't complain if it works as the original question posed:-)

[snip]

grep -A3 stuff is only for Gnu grep - which probably is a good idea to have.

Yuan Liu

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by mats.blomstr.. » Fri, 17 Jan 2003 18:47:24



> I'm having trouble searching for a specfic word and then returning the found
> line and the following 3 lines after that.

        bash$ cat test
             1  i am testing this script
             2  test
             3  test
             4  test
             5  this is not a test
             6  ntest
             7  ntest
             8  ntest
             9  i am testing this script again
            10  test
            11  test
            12  test
        bash$ echo "g/testing/ .,.+3 p" |ed -s test
             1  i am testing this script
             2  test
             3  test
             4  test
             9  i am testing this script again
            10  test
            11  test
            12  test
        bash$

//Mats

--
My code (if any) in this message are Copyright (C) 2003 Mats Blomstrand
and licensed under GNU GPL, http://www.gnu.org/licenses/gpl.html

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Stepan Kas » Fri, 17 Jan 2003 20:55:06


Hi,


> >awk '/testing/ || /again/{print $0; x=NR+1; y=NR+1; z=NR+1; print x; print
> y; print z}' filename

awk '/testing|again/{remain=4}; remain{remain--; print}'

HTH,
        Stepan

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Charles Dem » Sat, 18 Jan 2003 04:55:20




>Hi,


>> >awk '/testing/ || /again/{print $0; x=NR+1; y=NR+1; z=NR+1; print x; print
>> y; print z}' filename

>awk '/testing|again/{remain=4}; remain{remain--; print}'

Or this shorter form:

awk '/testing|again/{remain=4} remain--}'

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Stepan Kas » Sat, 18 Jan 2003 17:21:55


Hello Chuck,



> >awk '/testing|again/{remain=4}; remain{remain--; print}'

> Or this shorter form:

> awk '/testing|again/{remain=4} remain--}'

I was also thinking about a shorter form, but:
1) I was afraid I could make a mistake (I haven't tested my code);
2) I was afraid it would be less readable.

You probably haven't tested your code either: it contains a typo
(unmatched brace) and wouldn't work even if you remove that brace.

You'd need:

        awk '/testing|again/{remain=4} remain&&remain--'
or
        awk '/testing|again/{remain=4} remain-->0'

to get a working cryptic version.

Stepan

 
 
 

Grep/awk/sed for multiple/groups of lines in a file???

Post by Charles Dem » Sun, 19 Jan 2003 04:04:17




>Hello Chuck,



>> >awk '/testing|again/{remain=4}; remain{remain--; print}'

>> Or this shorter form:

>> awk '/testing|again/{remain=4} remain--}'

>I was also thinking about a shorter form, but:
>1) I was afraid I could make a mistake (I haven't tested my code);
>2) I was afraid it would be less readable.

>You probably haven't tested your code either: it contains a typo
>(unmatched brace) and wouldn't work even if you remove that brace.

Another correct observation!  :-)

Quote:>You'd need:

>    awk '/testing|again/{remain=4} remain&&remain--'
>or
>    awk '/testing|again/{remain=4} remain-->0'

Yup, forgot that -1 is a logical 1.  :-)

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

1. Grep/awk/sed for multiple/groups of lines in a file???

If you have GNU grep, you can print out lines of context (before,
after or both):

  $ grep -A 3 testing filename
  i am testing this script
  test
  test
  test
  --
  i am testing this script again
  test
  test
  test

If you don't have GNU grep, you can get it at

  http://www.gnu.org/software/grep/grep.html

I hope this helps,

Tim

2. AHA1542CF very slow

3. Problem: popclient shows password when someone 'ps -elf'

4. Taper and ftape

5. Extracting multiple lines OR deleting multiple lines from a file using AWK

6. Apache 1.1.1 httpd & cgi help

7. Using awk or the shell to parse a file containing multiple line groups of data

8. speed using grep/sed/wc/awk/shell_built_in to find number of lines

9. Fold multiple lines with sed or awk

10. Pattern Matching Multiple Lines with SED and AWK

11. Fold multiple lines with sed or awk