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

Display lines around matching lines

Status
Not open for further replies.

spookie

Programmer
May 30, 2001
655
IN
Hi,
I want to extract lines following matching pattern in a file with the matching line.
grep, sed will obviously give me only matching line and not lines following them.
Example
Code:
This is line1 - user1
This is line2 - user2
This is line3 - user3
I want two lines follwing line which contain user1 and the matched line.

How it can be achieved?


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Found this nifty sccript in "An AIX Companion"
------cut here------
#!/bin/ksh
#***************************************************************************
#* Description *
#* ----------------------------------------------------------------------- *
#* Allows you to search a file and return the specified item plus the next *
#* X number of specified lines. *
#***************************************************************************
#***************************************************************************
# Check to see if there are at least 3 arguments
#***************************************************************************
if (( $# < 3 ))
then
echo "Usage: $0 pattern lines file [files]"
exit 1
fi

#***************************************************************************
# Create the internal variables
#***************************************************************************
STRING=$1
LINES=$2
shift 2 # $* now contains only file names

#***************************************************************************
# Build the awk script by reading lines from this script
#***************************************************************************
cat > a.script <<-!
BEGIN { output=0; }
/$STRING/ { output=$LINES; }
{ if (output > 0) { print; output=output-1; } }
!

#***************************************************************************
# Invoke the awk script and erase it when done.
#***************************************************************************
awk -f a.script $*
rm a.script
-----end cut------

save it as "mgrep" (or any name you want), make it executable and run it like this:

mgrep pattern 3 /some/file

If pattern is found, it will display line pattern is on and 2 lines after pattern.
 
get gnu grep from sunfreeware.com

Best Regards, Franz
--
UNIX System Manager from Munich, Germany
 
Thanks for the info guys.

I will check that out.I have already checked Annihilannic solutions ( with little change i.e.sed -n '/user1/{p;n;p;n;p;q;}' <filename> :)) and that works fine.


--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top