Hi,
I need to search for a word in a file and need to extract exactly 10th line before and after that search string.
Awaiting your earlier updates. Thanks in advance.
Hi,
I need to search for a word in a file and need to extract exactly 10th line before and after that search string.
Awaiting your earlier updates. Thanks in advance.
check grep -A and grep -B
grep -A10 -B10 'word' file | sed '2,20d'
Thanks for your reply,
But i received below error message
grep -A10 -B10 "A00001" source1.txt | sed '2,20d'
grep: illegal option -- A
grep: illegal option -- 1
grep: illegal option -- 0
grep: illegal option -- B
grep: illegal option -- 1
grep: illegal option -- 0
Usage: grep -hblcnsviw pattern file . . .
Please correct me if i am wrong in giving the command
which grep are you using?
what is the version?
Try this:
awk '/searchstring/{for(i=0;i<10;i++)print a[(i+NR)%10];print;for(i=0;i<10;i++){getline;print}}{a[NR%10]=$0}' filename
cheers,
Devaraj Takhellambam
l=$(grep -n -N 1 "pattern" filename | cut -d":" -f1)
i -= 10
j += 10
sed -n "$j"p filename
sed -n "$i"p filename
nothing wrong in the way given, but only gnu grep supports these options.
Oh.... Thanks for your response. With your help, i framed the below code. There are two files. "file" contains the list of words to be searched in "file1". from file1, i need to extract 2nd line above the matched string and 10th line below the matched string. The below code is not working. Please correct my mistake and update me.
Thanks in advance
targetfile="/apps/file.txt"
targetfile1="/apps/file1.txt"
targetfile3="/apps/finallist.txt"
totalrecs=`sed -n $= $targetfile`
tot=1
cat $targetfile1 | while read c_line
do
ARRAY14[$tot]=`echo "$c_line"`
tot=$i+1
done
fet=1
while [ $fet -le $tot ]
do
l=`echo $(grep -n $ARRAY14[$fet] "$targetfile1" | cut -d":" -f1)`
i-=2
j+=10
sed -n "$j"p filename >> targetfile3
sed -n "$i"p filename >> targetfile3
fet=$fet+1
done