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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cut 5 lines from a file 1

Status
Not open for further replies.

nwo4life

Technical User
May 16, 2002
23
0
0
US
I need to search for ERROR in a file & cut the 5 lines after that. So it can be emailed.

ex. ERROR at line 1:
 
Using vi, in commmand mode, type '6dd' and it should delete the line you are on and the next 5...
 
sed -n '/ERROR/{n;p;n;p;n;p;n;p;n;p;}' myFile.log vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
What if I need the line with error on it as well as the next 5 lines. Thanks vgersh99 for the help.
 
nwo4life,

Here is a simple program I wrote to do exactly what you want. I programmed it for you so it runs interactively, but it is really simple and can be made to run without interaction with little effort. If you need help doing this, I can do it for ya..

#!/usr/bin/ksh

echo &quot;Enter search string: &quot;
read SRCHSTR

echo &quot;Enter full path to file to search: &quot;
read FILE

echo &quot;How many line after do you want?: &quot;
read NUMLINES

LINENUM=`grep -n &quot;$SRCHSTR&quot; $FILE | cut -f 1 -d :`

POS=0
while [ $POS -lt $NUMLINES ]
do
echo `grep -n . $FILE | grep ^\`expr $POS + $LINENUM\` | cut -f2,3,4,5,6,7 -d &quot;:&quot;`
POS=`expr $POS + 1`
done
 
with the 'line' - just add a 'p' to print it:

sed -n '/ERROR/{p;n;p;n;p;n;p;n;p;n;p;}' myFile.log vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top