Hi,
I have a requirement to search for a string in a large log file along with few lines before and after the the string. The following script was sufficient to search such an entry.
STRING_TO_GREP="$1"
FILE_TO_GREP="$2"
NUMBER_OF_LINES_BEFORE=$3
NUMBER_OF_LINES_AFTER=$4
for i in `grep -n "${STRING_TO_GREP}" $FILE_TO_GREP|cut -d ':' -f1`
do
echo "Line Number $i"
echo "--------------"
i1=$(($i-NUMBER_OF_LINES_BEFORE))
i2=$(($i+NUMBER_OF_LINES_AFTER))
if [ ${i1} -le 0 ]; then
i1=1
fi
sed -n "${i1},${i2}p" $FILE_TO_GREP
done
But my problem is that I do not know the value of NUMBER_OF_LINES_BEFORE and NUMBER_OF_LINES_AFTER beforehand, but needs to be identified based on the occurence of another string S1 just before the occurence of STRING_TO_GREP.
For example, if the file F1.txt contains:
a
b
c
d
e
f
g
h
i
j
c
I want to retrieve the following text based on the grep for 'f' with 'c' as the start of text and 'i' as the end. Please note that I do not want to include the c given in the end of file.
c
d
e
f
g
h
i
Thanks & Regards,
Praveen