Operating System - HP-UX
1827730 Members
2958 Online
109968 Solutions
New Discussion

Re: grep command - help needed

 
Anand_30
Regular Advisor

grep command - help needed

Is there any option in grep which helps me to search for a particular pattern and then display 'n' lines after that. I am using
HP-UX10.20. In linux it can be done using
'grep -A n' option.

Thanks,
Andy

9 REPLIES 9
A. Clay Stephenson
Acclaimed Contributor

Re: grep command - help needed

There is no such option in grep. It would be easy to do in awk. There is an extreme reluctance in UNIX (unlike Linux) to 'improve' commands which have literally been around for decades. It tends to make things very unportable.
If it ain't broke, I can fix that.
Anand_30
Regular Advisor

Re: grep command - help needed

Thanks,

How do I do it using awk. Can you provide some examples.

-Andy
curt larson_1
Honored Contributor

Re: grep command - help needed

cat yourfile |
awk '{
if ( $0 ~ /yourpattern/ ) {
for ( i=0; igetline;
print $0;
}}}'
Dave La Mar
Honored Contributor

Re: grep command - help needed

Andy -
As much as I would like to see the awk solution as well, this is a technique I have used.
You have the line number to start with from your grep -n so ....
sed -n "$your_line_number","$X_line_numbers_out"p $SCRIPT_HOME/$DATA_FILE > $OUT_FILE

Or let starting_line="$your_line_number + 1"
let ending_line="$your_line_number + 5"
sed -n "$starting_line","$ending_line"p $SCRIPT_HOME/$DATA_FILE > $OUT_FILE

Best of luck.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
H.Merijn Brand (procura
Honored Contributor

Re: grep command - help needed

Why not just fetch GNU grep for 10.20 from my site, and you have the same as Linux :)

https://www.beepz.com/personal/merijn/ , http://www.cmve.net/~merijn/ , or or http://www.hpux.ws/merijn/

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
curt larson_1
Honored Contributor

Re: grep command - help needed

have you tried

ex -s +"g/yourpattern/ .+1,.+numberOfLines |q" filename
curt larson_1
Honored Contributor

Re: grep command - help needed

oops forgot the p for print

ex -s +"g/yourpattern/ .+1,.+numberOfLines p|q" filename
Hein van den Heuvel
Honored Contributor

Re: grep command - help needed


Under OpenVMS the command would be:

$SEARCH/WINDOW=(0,)

The 0 being 0 pre-fix lines. Surely you were all dying to know that huh?


In Tru64 you can often use -p for a 'paragraph'. No use there either huh?


I'm a bit dissapointed at the lack of extension in many hpux tools. I like the suggestion of just picking up GNU grep.

In the mean time you coudl use AWK:

awk '//{print $0; i=; while (i--) {getline; print $0}}' <


or PERL (assuring a re-start every occurence:

cat | perl -e 'while (<>){$i= if (//); print if ($i-->0)}'



Jack Werner
Frequent Advisor

Re: grep command - help needed

Andy,

You can still use grep then pipe the stdout to head -n for number of lines to display.

grep file | head -20

Would display the first 20 lines grep returned.

CIAO
i'm retired