Contextual grep - Aix

This is a discussion on Contextual grep - Aix ; Hi, How I can have a grep with the context: for example 3 lines before and 3 lines after the pattern ( awk ? ) Thanks for your help......

+ Reply to Thread
Results 1 to 4 of 4

Thread: Contextual grep

  1. Contextual grep

    Hi,

    How I can have a grep with the context:
    for example 3 lines before and 3 lines after the pattern
    ( awk ? )

    Thanks for your help...



  2. Re: Contextual grep

    "astalavista" writes:

    > How I can have a grep with the context:
    > for example 3 lines before and 3 lines after the pattern
    > ( awk ? )


    Just use GNU grep:

    $ grep --version
    grep (GNU grep) 2.4.2

    $ grep -C3 lpd /etc/passwd
    uucp:!:5:5::/usr/lib/uucp:
    guest:!:100:100::/home/guest:
    nobody:!:4294967294:4294967294::/:
    lpd:!:9:4294967294::/:
    lp:*:11:11::/var/spool/lp:/bin/false
    nuucp:*:6:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
    imnadm:*:188:188::/home/imnadm:/usr/bin/ksh

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  3. Re: Contextual grep

    On Mar 25, 7:20 am, Paul Pluzhnikov
    wrote:
    > "astalavista" writes:
    > > How I can have a grep with the context:
    > > for example 3 lines before and 3 lines after the pattern
    > > ( awk ? )

    >
    > Just use GNU grep:
    >
    > $ grep --version
    > grep (GNU grep) 2.4.2
    >
    > $ grep -C3 lpd /etc/passwd
    > uucp:!:5:5::/usr/lib/uucp:
    > guest:!:100:100::/home/guest:
    > nobody:!:4294967294:4294967294::/:
    > lpd:!:9:4294967294::/:
    > lp:*:11:11::/var/spool/lp:/bin/false
    > nuucp:*:6:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
    > imnadm:*:188:188::/home/imnadm:/usr/bin/ksh
    >
    > Cheers,
    > --
    > In order to understand recursion you must first understand recursion.
    > Remove /-nsp/ for email.


    This works ; crude but no gnu needed:

    CNT=3;LN=`grep -n lpd /etc/passwd|cut -f1 -d:`; head -$(( LN+CNT )) /
    etc/passwd|tail -$(( 2*CNT+1 ))
    uucp:!:5:5::/usr/lib/uucp:
    guest:!:100:100::/home/guest:
    nobody:!:4294967294:4294967294::/:
    lpd:!:9:4294967294::/:
    lp:*:11:11::/var/spool/lp:/bin/false
    invscout:*:200:1::/var/adm/invscout:/usr/bin/ksh
    snapp:*:201:12:snapp login user:/usr/sbin/snapp:/usr/sbin/snappd


    script it with $CNT and $FILE as comand line variables and it would
    give a reasonable facsimile -- at least on smaller files


  4. Re: Contextual grep

    Thanks very much ...
    it's what I needed ...
    >
    > This works ; crude but no gnu needed:
    >
    > CNT=3;LN=`grep -n lpd /etc/passwd|cut -f1 -d:`; head -$(( LN+CNT )) /
    > etc/passwd|tail -$(( 2*CNT+1 ))
    > uucp:!:5:5::/usr/lib/uucp:
    > guest:!:100:100::/home/guest:
    > nobody:!:4294967294:4294967294::/:
    > lpd:!:9:4294967294::/:
    > lp:*:11:11::/var/spool/lp:/bin/false
    > invscout:*:200:1::/var/adm/invscout:/usr/bin/ksh
    > snapp:*:201:12:snapp login user:/usr/sbin/snapp:/usr/sbin/snappd
    >
    >
    > script it with $CNT and $FILE as comand line variables and it would
    > give a reasonable facsimile -- at least on smaller files
    >




+ Reply to Thread