Link to home
Start Free TrialLog in
Avatar of sunhux
sunhux

asked on

grep that displays 4 lines above & 1 line below of what's found

I'm on Solaris 10  &  RHEL6.


I'll need to display 4 lines above & 1 line below of the output of the command below:
How can this be done without installing additional packages?

# grep "Found" /opt/av/scanlog.txt | grep -i "iruses"  


 ^MScanning /opt/av/eicar/eicar.com->Found Virus [Eicar_test_file]

^M

 Found 1 files containing viruses.
SOLUTION
Avatar of BharathKumarRaju DasaraRaju
BharathKumarRaju DasaraRaju
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux
sunhux

ASKER

Note the search string as the following are the 3 types of lines I'm looking for in the log:

 /app/files/eyecar.jpg->Found Viruses [Eicar_test_file]         (ie "Found Viruses"
Found 1/25 Viruses in /app/weblogic/ww/esvcs/data/out/sent/APPL_09.zip   (ie Found ... Viruses)
Found 1 files containing viruses.        (Found ... viruses)
Avatar of sunhux

ASKER

What if I need 4 lines above & 1 line below (ie not 5 lines below) ?
Avatar of sunhux

ASKER

Your syntax doesn't seem right:

-bash-3.2# grep -iC5 "Found" x* |more
grep: illegal option -- C
grep: illegal option -- 5
Usage: grep -hblcnsviw pattern file . . .
SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Solaris' grep does not support -A, -B or -C, so try

FILE=/opt/av/scanlog.txt
grep -Ein "Found.*viruses"  $FILE |awk -F: '{print $1}' | while read N
do
     sed -n "$((N-4)),$((N+1))p" $FILE
done

Gerwin's solution above is just fine for RHEL6!
Avatar of sunhux

ASKER

http://superuser.com/questions/688320/print-line-x-lines-before-after-found-line
Yep, the above link would not work on Solaris though likely to work on RHEL

(extracted from above link:
  sudo lspci -vnn | grep -i net -A 12 )

Got a syntax error:

-bash-3.2# FILE=/opt/av/update.log
-bash-3.2# grep -Ein "Found.*viruses"  $FILE |awk -F: '{print $1}' | while read N
> do
>      sed -n "$((N-4)),$((N+1))p" $FILE
> done
grep: illegal option -- E
Usage: grep -hblcnsviw pattern file . . .
Avatar of sunhux

ASKER

Solaris has 2 greps, let me know which one to use:

usr/bin/grep
usr/xpg4/bin/grep
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunhux

ASKER

Excellent, exact case is needed.  I'll test it out tomorrow Sat & if no issue,
will close this thread