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->F ound Virus [Eicar_test_file]
^M
Found 1 files containing viruses.
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->F
^M
Found 1 files containing viruses.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
What if I need 4 lines above & 1 line below (ie not 5 lines below) ?
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 . . .
-bash-3.2# grep -iC5 "Found" x* |more
grep: illegal option -- C
grep: illegal option -- 5
Usage: grep -hblcnsviw pattern file . . .
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!
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!
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 . . .
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 . . .
ASKER
Solaris has 2 greps, let me know which one to use:
usr/bin/grep
usr/xpg4/bin/grep
usr/bin/grep
usr/xpg4/bin/grep
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent, exact case is needed. I'll test it out tomorrow Sat & if no issue,
will close this thread
will close this thread
ASKER
/app/files/eyecar.jpg->Fou
Found 1/25 Viruses in /app/weblogic/ww/esvcs/dat
Found 1 files containing viruses. (Found ... viruses)