Can some one help me to print 4th line before the match using egrep or grep command options.
i have a very large file and i need to search the entire file, look for the match (key word) and print 4th line before the matched key word.
[house@leonov] cat test.file
four
three
two
one
gotcha
[house@leonov] grep -B4 'gotcha' test.file | head -n1
four
Try...
awk '$0~s{print r[NR%b]}{r[NR%b]=$0}' b=4 s="keyword" file1
while read line
do
echo $line | grep $pattern >/dev/null
if [ $? -eq 0 ]
then
echo $line4
fi
line4=$line3
line3=$line2
line2=$line1
line1=$line
done < $filename
1 Like
thanks to you all, i am looking single line command and i am getitng error for first two replies.
grep: illegal option -- B
grep: illegal option -- 4
Usage: grep -hblcnsviw pattern file . . .
-----------------------------------------------------
ksh: -ksh~s{print: not found
awk: syntax error near line 1
awk: bailing out near line 1
------------------------------------------------------
Make sure that you use the 'single quotes' around the awk code - as posted.
hi,i tried with that option also. still getting error. below is my exact command.
# awk '$0~s{print r[NR%b]}{r[NR%b]=$0}' b=4 s="0x9f 0x81 0x19 0x01 0x02" test2.log | pg
awk: syntax error near line 1
awk: bailing out near line 1
there are some spaces in the key word am searching, will that matter?
Use nawk or /usr/xpg4/bin/awk on Solaris.
If on Solaris, try using nawk.
Thanks you very much !!!!, nawk worked.