Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-06-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
print line that matches and next line, too

I'm using sh on hp-ux and want to find / print a line that matches 132.101- and the next line, too. grep -A isn't supported on hp-ux, so I'm trying awk and sed.

The code below works but only prints the first occurence. I need all matches from the file.


Code:
awk '/132.101-/{f=2;print;next}f{print;exit}' INFILE1 >outfile1

I've also tried sed but this cannot be parsed, my shell tells me.

Code:
sed -n -e '/132.101-/{N;p}' INFILE1 >outfile1

Thanks for your help!
Sponsored Links
    #2  
Old 07-06-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 476 Times in 456 Posts

Code:
awk '/132\.101-/{print;getline;print}' INFILE1 > outfile1

Following are the issues with your awk:


Code:
awk '/132.101-/{f=2;print;next}f{print;exit}' INFILE1 >outfile1

1) The regex is not correct (although it will work). . is a regex metacharacter which matches any character. So, to match a literal . , you'll need to escape the period.

2) The exit statement will make awk stop after it has printed out the first line after the one containing "132.101-". Hence, you are getting only 2 lines.

If you want to use your awk, you'll need to modify it a bit:


Code:
awk '/132.101-/{f=2;print;next}f{print;f=0}' INFILE1 >outfile1


Last edited by elixir_sinari; 07-06-2012 at 05:23 PM..
The Following User Says Thank You to elixir_sinari For This Useful Post:
Scottie1954 (07-06-2012)
Sponsored Links
    #3  
Old 07-06-2012
Registered User
 
Join Date: Jul 2011
Posts: 23
Thanks: 8
Thanked 0 Times in 0 Posts
That worked! Thanks a bunch. Never thought that adding something straightforward like ;getline; would be parsed by hp-ux.

I also changed my pattern a bit to find another potential match:

Code:
awk '/132\.10[1-2]-/{print;getline;print}' INFILE1 >outfile1

---------- Post updated at 04:16 PM ---------- Previous update was at 02:25 PM ----------

Just for argument's sake, how would I do the opposite -- pull the line matching the pattern and the previous line? Thanks.

---------- Post updated at 04:42 PM ---------- Previous update was at 04:16 PM ----------

I found an answer after some more digging:


Code:
awk '/132\.101-/{print x;print};{x=$0}' INFILE1 >outfile1

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print line if subsrt matches array phpsnook UNIX for Advanced & Expert Users 2 08-09-2011 02:52 AM
How to print line if field matches? stinkefisch Shell Programming and Scripting 7 09-03-2010 09:45 AM
Print word 1 in line 1 and word 2 in line 2 if it matches a pattern bangaram Shell Programming and Scripting 7 08-31-2009 05:58 AM
Print line if first Field matches a pattern Raynon Shell Programming and Scripting 2 01-08-2009 05:07 AM
print next line if matches a particular word..need help anish19 Shell Programming and Scripting 2 06-12-2008 05:06 AM



All times are GMT -4. The time now is 06:19 AM.