Computing.Net > Forums > Unix > print the next line

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

print the next line

Reply to Message Icon

Name: vicchai
Date: June 4, 2004 at 18:39:15 Pacific
OS: hp unix
CPU/Ram: nil
Comment:

Hi All,

How can I find a word and print that line and the next line as well?

I know this:
awk '{if ($2=="words") {print ab,"\n",$0} {ab=$0}}' file.txt
well print the line found with this "words" in field 2 and the above line as well, but how can I print the line after the "words"?
Or is there a more flexible way to do it like looking a word in a text file with no field and print a few line above and below that word.

Thanks.




Sponsored Link
Ads by Google

Response Number 1
Name: thepubba
Date: June 4, 2004 at 21:09:23 Pacific
Reply:

If your version of grep support the -A option, this should work fine:

grep -A 1 <word> input.file


0

Response Number 2
Name: vicchai
Date: June 5, 2004 at 20:26:58 Pacific
Reply:

Hi Jerry Lemieux,

Thanks for your reply, but my version of grep not support the -A option, I'm using HP unix B.10.20. Is there any other way to do it?


0

Response Number 3
Name: Dlonra
Date: June 6, 2004 at 14:46:12 Pacific
Reply:

you probably don't have the -C grep option?
else
ex -R File<<!
> g/pattern/.-1,.+1p
> q
> !


0

Response Number 4
Name: vicchai
Date: June 7, 2004 at 19:34:52 Pacific
Reply:

Hi Dlonra,

Thanks for your help, but how can I catched the out put to a file? I have read the man page of this ex, I have try something like this:
ex File<<!
>g/pattern/.-1,.+1p
>w File_out
>!
But the File_out data is the same as the File, why can I make it only contain the data that I want. Thanks again!


0

Response Number 5
Name: Dlonra
Date: June 8, 2004 at 06:52:53 Pacific
Reply:

>File_out
ex File<<!
>g/pattern/.-1,.+1w>>File_out
>q
>!

OR
ex -R File<<! >File_out
> g/pattern/.-1,.+1p
> q
> !


0

Related Posts

See More



Response Number 6
Name: vicchai
Date: June 8, 2004 at 20:59:44 Pacific
Reply:

Hi Dlonra,

Thanks a lot! It works great. Dlonra, you are really amazing!!!


0

Response Number 7
Name: Jim Boothe
Date: June 11, 2004 at 06:56:39 Pacific
Reply:

Victor, since you use awk, be aware that getline will immediately read the next line. So when you locate your desired line, to print the current and following line, just do:

   {print
    getline
    print}


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: print the next line

PERL script www.computing.net/answers/unix/perl-script/6183.html

Nested while loop www.computing.net/answers/unix/nested-while-loop/7684.html

grep - print next line too www.computing.net/answers/unix/grep-print-next-line-too/7417.html