Operating System - HP-UX
1753884 Members
7763 Online
108809 Solutions
New Discussion юеВ

using sed or grep print 5 lines of context before and after regexp

 
ankurp
Frequent Advisor

using sed or grep print 5 lines of context before and after regexp

Hi friends,

Using sed or grep, print 5 lines of context before and after regexp in hp ux.
As -A option is not available in hp ux.

rgds
Ankur
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: using sed or grep print 5 lines of context before and after regexp

From "Handy One-Liners for SED" (attached):

# print 1 line of context before and after regexp, with line number
# indicating where the regexp occurred (similar to "grep -A1 -B1")
sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h



Pete

Pete
ankurp
Frequent Advisor

Re: using sed or grep print 5 lines of context before and after regexp

hi pete,

i do have that , but how do i have 5 lines before the regex.


rgds
Ankur
Peter Godron
Honored Contributor

Re: using sed or grep print 5 lines of context before and after regexp

Ankur,
what grep do you use?

With gnu grep it's:
grep -A5 -B5 data file.lis

Regards
Anupam Anshu_1
Valued Contributor

Re: using sed or grep print 5 lines of context before and after regexp

Hi Ankur,

As of now, HP-UX grep(1) doesn't support the context printing like 5 lines + and -.

It will be sometime before HP supports such an option.

BTW, which version of HP-UX you are using?

Regards,

Anupam Anshu
Pete Randall
Outstanding Contributor

Re: using sed or grep print 5 lines of context before and after regexp

Ankur,

I've been trying to decypher exactly how the example I gave you works and it appears to me that there's no way to go back 5 lines. It just swaps current and previous, meaning it can only go back 1 line.

I guess gnu grep would be the answer then:http:

//hpux.cs.utah.edu/hppd/hpux/Gnu/grep-2.5.1/


Pete

Pete
curt larson_1
Honored Contributor

Re: using sed or grep print 5 lines of context before and after regexp

or if you don't mind using perl:
http://www.cpan.org/scripts/text-processing/cgrep
Biswajit Tripathy
Honored Contributor

Re: using sed or grep print 5 lines of context before and after regexp

Here is a script that should do what you want. The
number of lines to print (5 in your example), input
file name and search pattern at set at the top so
that you can change them. Consider modifying the
script to use command line parameters. The script
will print 11 lines if pattern matches (i.e 5 previous
and 5 following lines and the line itself that matches.

NOTE : The script does not behave nicely if the
patter matches first or last 5 lines. Modifying the
script to do that is left as an exercise to original
poster ;-)

------ Start -----
#!/usr/bin/ksh
# Set the following parameters
integer LINES_TO_PRINT=5
INPUT_FILE_NAME="testfile"
SEARCH_PATTERN="stu"

# Executation starts from here
LINES_TO_READ=$(echo \
"2*$LINES_TO_PRINT+1" | bc )
exec 3<$INPUT_FILE_NAME
integer i=1
while [ /bin/true ]
do
if [ $i -gt $LINES_TO_READ ]
then
break
fi
read -ru3 line[$i]
i=$i+1
done
integer MIDDLE_LINE=$LINES_TO_PRINT+1
while [ /bin/true ]
do
integer j=1
echo ${line[$MIDDLE_LINE]} | grep -q \
$SEARCH_PATTERN
if [ $? -eq 0 ]
then
while [ $j -le $LINES_TO_READ ]
do
echo ${line[$j]}
j=$j+1
done
fi
j=1
while [ $j -lt $LINES_TO_READ ]
do
line[$j]=${line[$j+1]}
j=$j+1
done
read -ru3 line[$j]
if [ $? -ne 0 ]
then
exit
fi
done

- Biswajit
:-)
john korterman
Honored Contributor

Re: using sed or grep print 5 lines of context before and after regexp

Hi,
the attached script may do it: try running it like this:
# print_before_after.sh "a quoted string" 5

The script prints out line numbers as well. To to avoid that, change the printing line (looks almost like the one below!) to:
cat -n $3 | awk -v lineb=$LINE_BEFORE -v linea=$LINE_AFTER '$1==lineb,$1
==linea {$1="";print $0}'

regards,
John K.



it would be nice if you always got a second chance
ankurp
Frequent Advisor

Re: using sed or grep print 5 lines of context before and after regexp

wow, thanks for all that responce and help.
will be tryin all ur scripts ( debugging them to know them better)
Will do whatever i can to help u all.
thanks once again.

rgds
Ankur