Re: How to display n rows before and after grep ?

From: Michael Heiming (michael+USENET_at_www.heiming.de)
Date: 02/23/05

  • Next message: Dale DeRemer: "multiple .profile edits"
    Date: Wed, 23 Feb 2005 11:24:28 +0100
    
    

    In comp.unix.shell fmarchioni@libero.it:
    > Hi all,
    > with most Linux distributions I can use

    > grep -n or
    > grep -C

    > to display n rows -before and after- the lines intercepted by grep.

    No, those switches have another meaning in GNU grep, you probably
    mean "-A"/"-B".

    > Unfortunately I cannot find this option on Solaris.
    > (I'm using SunOS ultra450 5.8 Generic_108528-27 sun4u sparc
    > SUNW,Ultra-4)

    > Does anybody know an hack to achieve it, anyway ?
    > (egrep, fgrep ... ?)

    You can install GNU grep on Solaris, there should be ready to
    install packages from Sun available (sunfreeware.com or so).

    Alternatively 'ex' should work:

    ex -s file <<_HERE_
    /match/-3;+3p
    q
    _HERE_

    Or use awk (Perhaps nawk, the awk in $PATH on solaris is
    unusable, but there are two others available and mostly
    installed)

     awk '/match/ {k=4} k {k--;print}' file

    Or use 'sed':

     sed -n '/string/{N;N;N;N;p;}' file

    -- 
    Michael Heiming (X-PGP-Sig > GPG-Key ID: EDD27B94)
    mail: echo zvpunry@urvzvat.qr | perl -pe 'y/a-z/n-za-m/'
    #bofh excuse 224: Jan  9 16:41:27 huber su: 'su root' succeeded
    for .... on /dev/pts/1
    

  • Next message: Dale DeRemer: "multiple .profile edits"

    Relevant Pages

    • Re: finding files which do NOT contain a pattern
      ... sort. ... pattern, then copy them to a different directory. ... Gnu grep has a -L option to find files whose contents do not ... and install it locally if you do not already have it. ...
      (comp.unix.shell)
    • Re: "grep" several lines
      ... > OS is Solaris 8. ... > How is that done the easiest way, eg. using sed or awk? ... GNU grep. ...
      (comp.unix.shell)
    • Re: finding files which do NOT contain a pattern
      ... sort. ... pattern, then copy them to a different directory. ... Gnu grep has a -L option to find files whose contents do not ... and install it locally if you do not already have it. ...
      (comp.unix.shell)
    • Re: Unix commands implemented ? (grep, wc, cat...)
      ... which I don't think anyone has done in java. ... implementation of GNU grep in the gnu.regexp.util package: ... That might also give you some sed and awk functionality according ... syntax, not the complete sed or awk language syntax. ...
      (comp.lang.java.programmer)
    • Re: Die Zeile davor!
      ... Wie bekomme ich das Interface heraus auf dem die IP 10.253.156.50 definiert ... GNU grep kennt da eine Option: ... Auf Solaris findet man GNU grep eher Selten. ...
      (de.comp.os.unix.shell)