Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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
ijklThanks.

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...:)

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

![]() |
![]() |
![]() |

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