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
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.
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
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:
Summary: I need a perl script to open a file in a directory, search for a string, then print the next line after the searched string and close the file. does anyone now how 2 do this or lead me to the proper ...
Summary: Hello, I have a file containig DHCP lease information like the following: 02/01/2007 9:57:32 name/dhcp/1 Activity Protocol 0 04995 10.10.10.12 Lease renewed to MAC: '1,6,00:09:ef:00:32:09' packet 'R...
Summary: 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 scri...