Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

greping a line with <string> + 3 lines

Status
Not open for further replies.

MoshiachNow

IS-IT--Management
Feb 6, 2002
1,851
IL
Hi,

I have a 10 MB text file,no space/special paragraph marks between the lines.

I need to grep out lines with a <string> and 3 lines after this line .

How will I do it ? &quot;Long live king Moshiach !&quot;
h
 
Try this script.

#!/bin/ksh
counter=0
for line in `cat your_filename`
do
let &quot;counter=counter+1&quot;
echo $line > temp_file
grep 'your_string' temp_file > dummy_file
if [[ $? = 0 ]] then
echo &quot;String found in line &quot;$counter
tail +$count your_filename | head -4
echo
fi
done

This script reads each line of the file and puts it into a temporary file. It then greps for the required string. The output from grep is not required so is put into a dummy file. If grep is successful then tail and head are used to display the line and the following three.

hope you find this useful.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top