Script works with Linux not with Solaris

Hi I have the following script which works in Linux shell but gives issues with Sun OS Solaris 5.10,

What i am trying to achieve here is we have a list of file names in list.txt file and we parse each file at a time for a particular pattern and copt next 4 lines after we hit the pattern to a file called report.txt

#!/bin/ksh 

grep ^Table results.txt  | grep :$ > $HOME/list.txt
 
cat $HOME/list.txt | while read line
 
do
 
echo $line
echo "***********************************" >>$HOME/Report
 
grep -A 5 "$line" results.txt >>$HOME/Report
 
echo "**********************************" >>$HOME/Report
 
done
 

when we use grep -A we get illegal option error, we used grep in /usr/xpg4/bin/grep still we face same issue and When we uncomment awk and use it the file prints nothing.

Please help

-A is a GNUism. Switch to awk, or use /usr/sfw/bin/ggrep.

GNU Grep was included with Oracle Solaris 10 1/13

This (perhaps with a minor tweaking) should work for you:
Replace

grep -A 5 "$line" results.txt >>$HOME/Report

with

awk -v line=$line '/'$line'/&&p=1;p&&p++>1&&p<7' results.txt >> $HOME/Report

That will redirect the line that matches the pattern and the next four lines to the file $HOME/Report
If you need further help, do not hesitate to let us know.

awk -v line=$line '($0 ~ line)&&p=1;p&&p++>1&&p<7' results.txt >> $HOME/Report

Almost nailed it, though ;).

Corona688 and gacanepa both missed a key point here. Unless they know that Yugendra's $PATH setting includes /usr/xpg4/bin or /usr/xpg6/bin before /bin and /usr/bin, they should explicitly use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk in this script for it to work on Yugendra's Solaris systems.

2 Likes