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/>Found Virus [Eicar_test_file]


 Found 1 files containing viruses.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BharathKumarRaju DasaraRajuDevops EngineerCommented:
user -C5...

grep -iC5 "sample" <<file_name>>

which displays above 5 lines and below 5 lines of matched text
sunhuxAuthor Commented:
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/   (ie Found ... Viruses)
Found 1 files containing viruses.        (Found ... viruses)
sunhuxAuthor Commented:
What if I need 4 lines above & 1 line below (ie not 5 lines below) ?
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

sunhuxAuthor Commented:
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 . . .
Gerwin Jansen, EE MVETopic Advisor Commented:

grep -A 4 -B 1 "Found" /opt/av/scanlog.txt
Solaris' grep does not support -A, -B or -C, so try

grep -Ein "Found.*viruses"  $FILE |awk -F: '{print $1}' | while read N
     sed -n "$((N-4)),$((N+1))p" $FILE

Gerwin's solution above is just fine for RHEL6!
sunhuxAuthor Commented:
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 . . .
sunhuxAuthor Commented:
Solaris has 2 greps, let me know which one to use:

Sorry, forgot to say that it's /usr/xpg4/bin/grep

And here is a version which can handle matches in the first few lines of a file:

/usr/xpg4/bin/grep -Ein "Found.*viruses"  $FILE |awk -F: '{print $1}' | while read N
     S=$((N-4)); [[ S -le 0 ]] && S=1
     sed -n "${S},${E}p" $FILE

If exact case matching is a concern (e.g. match "Found" but not "found") replace the above "grep" line with this one:

/usr/xpg4/bin/grep -En "Found.*[vV]iruses"  $FILE |awk -F: '{print $1}' | while read N

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunhuxAuthor Commented:
Excellent, exact case is needed.  I'll test it out tomorrow Sat & if no issue,
will close this thread
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.