The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 12:31 PM
Shell Script: want to insert values in database when update script runs ring Shell Programming and Scripting 1 10-25-2007 12:06 AM
here document to automate perl script that call script hogger84 Shell Programming and Scripting 3 10-22-2007 07:15 AM
returning to the parent shell after invoking a script within a script gurukottur Shell Programming and Scripting 5 09-26-2006 04:05 AM
return valuse from child script to parent script borncrazy Shell Programming and Scripting 1 08-20-2004 12:39 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-19-2005
Registered User
 

Join Date: Apr 2005
Posts: 17
Awk script help

I want to search a file where the 4th field begins with 03: and then print the line and the following 14 lines.
I can find the 4th field using awk but then I just get the same line repeated 14 times.
BEGIN {FS=OFS=" "}
{
tsttxt=substr($4,1,3)
if( tsttxt == "03:" )
{print
for (i = 1; i <= 13; i++)
print
}
}


Any Ideas welcome.

thanx
Reply With Quote
Forum Sponsor
  #2  
Old 09-19-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
Quote:
Originally Posted by Mudshark
I want to search a file where the 4th field begins with 03: and then print the line and the following 14 lines.
I can find the 4th field using awk but then I just get the same line repeated 14 times.
BEGIN {FS=OFS=" "}
{
tsttxt=substr($4,1,3)
if( tsttxt == "03:" )
{print
for (i = 1; i <= 13; i++)
print
}
}


Any Ideas welcome.

thanx
You need to add getline to your for loop.
Code:
BEGIN {FS=OFS=" "}
{
tsttxt=substr($4,1,3)
if( tsttxt == "03:" )
{print
for (i = 1; i <= 13; i++) {
    getline
    print
}

}
}
Reply With Quote
  #3  
Old 09-19-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
Quick explanation: "print" is only working on the current input line; "getline" switches the context to the next input line.
Reply With Quote
  #4  
Old 09-19-2005
Registered User
 

Join Date: Apr 2005
Posts: 17
Thanks tmarikle -
Its close but not quite - I now get the line with 03: in the 4th field and then just the 13th line after it.
I tried moving the {} around but to no good effect. I need the getline and the print $0 to act together ..

BEGIN {FS=OFS=" "}
{
tsttxt=substr($4,1,3)
if( tsttxt == "03:" )
{print $0
for (i = 1; i <= 13; i++)
getline
print $0
}
Reply With Quote
  #5  
Old 09-19-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
You missed the braces { } that I added to your for loop so that both "getline" and "print" are performed for each loop iteration.
Reply With Quote
  #6  
Old 09-19-2005
Registered User
 

Join Date: Apr 2005
Posts: 17
Working At Last

I think this works

BEGIN {FS=OFS=" "}
{
tsttxt=substr($4,1,3)
if( tsttxt == "03:" )
{print $0
for (i = 1; i <= 13; i++)
{ getline ;print $0 }
}
}
Reply With Quote
  #7  
Old 09-19-2005
Registered User
 

Join Date: Jan 2005
Posts: 682
The script should work as I modified it. "print $0" is the same as "print" in your script's case. The missing element when you tested it was the braces following the "for" loop.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0