Computing.Net > Forums > Unix > grep - print next line too

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.

grep - print next line too

Reply to Message Icon

Name: Venu
Date: May 22, 2006 at 23:41:18 Pacific
OS: hpux11i
CPU/Ram: 8gb
Product: hp
Comment:

Hi Guru's,

I would like to grep a particular string in a file and the output should print the line containing the grep'd string and next line also. Can this be done using native grep or thru some script ? Thanks for your help in advance

Cheers

ex:

--- start of file foo ----
abcd
efgh
ijkl
mnop
qrst
uvwx
--- end of file foo ----

if i grep for efgh in foo

grep -i efgh foo ( should return )

efgh
ijkl

Thanks.



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: May 23, 2006 at 03:27:28 Pacific
Reply:

if you have Python on your HPUX (i suppose)
you can do this:
f = open(your_file)
while 1:
line = f.readline()
if line == '': break
if "efgh" in line:
print line
print f.readline()
f.close()

Note that awk, sed , perl...even the shell can be used to do what you want. But i am not familiar with them..so just wait for replys from other gurus...:)


0

Response Number 2
Name: nails
Date: May 23, 2006 at 09:33:19 Pacific
Reply:

If you have access to GNU grep (i.e. Linux. see gnu.org to download it for your *nix version), it contains an after context, -A, and before context, -B, options.

So with GNU grep you could do:

# untested
grep -A 1 -i ...

Of course, you can always write a script like ghostdog.

This link has a context script:

http://www.unixreview.com/documents/s=9455/ur0412b/ur0412b.html



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







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: grep - print next line too

Find a string; print next line www.computing.net/answers/unix/find-a-string-print-next-line/7661.html

print the next line www.computing.net/answers/unix/print-the-next-line/6313.html

script which prints certain lines www.computing.net/answers/unix/script-which-prints-certain-lines-/6563.html