#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11

    Unhappy Displaying lines above grep-ed line


    Hi,

    How do i display 15 lines above the line that i grep for? Lets say i grep for "searchstring" in a flat file, i want to display 15 lines before the "searchstring" line. is ther a way to do this? Any help is appreciated. Thx in advance.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Posts
    121
    Rep Power
    12
    There is a program called cgrep (for "context grep") that can do this. Here is a link. Go to that page and look for the cgrep links.

    Also here is a perl implementation, and a shell script.

    I've never used any of these, but I think they can do pretty much what you're looking for.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    Hi Perderabo,

    I wish i could use the program, but i cant. This server i am working on is a production server and i can install any new programs on it .Is ther any other way i can do this?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    I was also wondering if i could display to the standard output cretain lines based on their line numbers. For example i could grep a line with its line number using the -n option then i could loop 15 lines before that and display those line. So lets say i grep the line at line number 42, i would -1 the line number and ouput the line at line 41, then -1 again and display line 40....and so on. Is there a way to display string based on specific line numbers? Thnx.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    you have a couple of probls
    - almost unix tools, works sequentailly, so processing line x
    they have no idea about line x-1
    - what when before 'searchstring' there are NOT 15 lines ?
    use awk, perl or c

    read the file in an array
    if you found 'searchstring' at line-number >14
    print elements of array[pos-15,pos]
    restart the job
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    I finally managed to solve the line no problem. I grep-ed the line with the -n option and wrote the output to a file and grepped the line nos and did a while loop to read up and down of the grep-ed line. A bit slow but what the hell....it works
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Posts
    1
    Rep Power
    0
    did u try grep -A (after) or grep -B (before) ?
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    a tricky but fast(er) way:
    1) get line number (grep)
    2) calculate -= START, if negative skipp it (bc)
    3) generate a sed script (echo)
    4) if successfull, exec it (sed)


    Code:
    #!/usr/bin/sh
    
    FILE=afile
    START=3
    SEARCH=aaa
    
    rm -f ${SED:=sedcmd}
    
    (
       for num in `grep -n $SEARCH $FILE|sed 's/:.*//'`
       do
           start=`echo "s=$num-$START;if(s >= 0) s;"|bc`
           [ x$start = x ] && continue
           echo $start,${num}p
       done
    ) >$SED
    [ -s $SED ] && sed -nf $SED $FILE
    exit 0
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    Hi guggach,

    for some strange reason, it dosent display anything if there is only one occurance of the word being grep-ed. And if the word is sandwiched between othe chars, it is not being displayed either.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    hi mtrxreloaded,

    I cant seem to find the -A or -B option for grep. Is that what you meant?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    36
    Rep Power
    11
    anyway.....this is my code
    <code>
    cat /tmp/vinod/linenos | while read LINE
    do
    lineno=$LINE
    count=0
    actline=0
    lineend=`expr $lineno + 3`
    linestr=`expr $lineno - 1`
    actline=$lineno
    echo >> results.txt
    echo >> results.txt
    while [ "$count" != "3" ]
    do
    actline=`expr $actline - 1`
    grep $actline /tmp/vinod/searchtext >> /tmp/vinod/results.txt
    count=`expr $count + 1`
    if [ "$count" = "3" ];
    then
    grep $lineno /tmp/vinod/searchtext >> /tmp/vinod/results.txt
    count=0
    actline=$lineno
    while [ "$count" != "3" ]
    do
    actline=`expr $actline + 1`
    grep $actline /tmp/vinod/searchtext >> /tmp/vinod/results.txt
    count=`expr $count + 1`
    done
    fi
    echo >> /tmp/vinod/results.txt
    echo >> /tmp/vinod/results.txt
    done

    done

    </code>
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14
    -A -B is maybe gnu-grep.
    you have solid shell misunderstandings

    do you know the difference:
    echo >> /tmp/vinod/results.txt
    echo >> /tmp/vinod/results.txt

    and:
    (
    echo
    echo
    ) >/tmp/vinod/results.txt

    put this in afile:

    hallo
    gugus
    aaa
    123
    456
    789
    aaa
    222
    333
    444
    aaa
    lollo
    aaa

    and try the script, it must print:

    123
    456
    789
    aaa
    222
    333
    444
    aaa

    every thing else is cr...
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    1
    Rep Power
    0
    Originally Posted by Vinod V
    hi mtrxreloaded,

    I cant seem to find the -A or -B option for grep. Is that what you meant?
    GREP --help

    Context control:
    -B, --before-context=NUM print NUM lines of leading context
    -A, --after-context=NUM print NUM lines of trailing context

    exp: grep -A 4 "string"
    - prints 4 lines after the matching string
    exp: grep -B 4 "string"
    - prints 4 lines before the matching string
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2011
    Posts
    1
    Rep Power
    0
    One line command to display 5 lines above and 5 lines below the grep-ed line:

    grep -n "<pattern>" testfile.txt | awk -F":" '{print $1-5 "," $1+5 "p"}' | xargs -t -i sed -n {} testfile.txt

IMN logo majestic logo threadwatch logo seochat tools logo