Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

while loop in AWK script

Status
Not open for further replies.

Bungheid

Technical User
Nov 14, 2002
16
0
0
US
The first version of the awk script uses a while !~ so that I read my file until the next block, block id = 'RESPN'.

This code just hangs in the middle of writing the file.

The second version specifies the records that are in each block and uses an equals to rather than a not equals- this works fine- any ideas why the not equals hangs half way through the file?


Command line

awk -f get_RRJ_REJ.sh FLAG='"REJCT"' /appl/out/archive/rnj/GTM01.PN00000.RNJ >> RJT_RRJ.txt

Version 1

BEGIN { FS="," }
{
z = 1
while ($1 ~ /RESPN/) {
rec[1] = $0
x = 1
getline
while ($1 !~ /RESPN/) {
++x
rec[x] = $0
if ($3 == FLAG) {
z = 2
}
getline
}
if (z == 2) {
for ( k = 1; k <= x; ++k) {
print rec[k]
}
if ($1 ~ /TRAIL/){
exit
}
}
}
}

Version 2

BEGIN { FS="," }
{
z = 1
while ($1 ~ /RESPN/) {
rec[1] = $0
x = 1
getline
while ($1 ~ /TRANS/ || $1 ~ /TROUT/ || $1 ~ /MTPNT/ || $1 ~ /REJRS/ || $1 ~ /ASSET/) {
++x
rec[x] = $0
if ($3 == FLAG) {
z = 2
}
getline
}
if (z == 2) {
for ( k = 1; k <= x; ++k) {
print rec[k]
}
if ($1 ~ /TRAIL/){
exit
}
}
}
}

 
Why don't you test the return value of the getline calls ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
In Version 1, because you don't test for success on getline, the inner loop can't exit until $1 ~ /RESPN/ and the outer loop can't exit until either $1 !~ /RESPN/ or $1 ~ /TRAIL/.

Therefore:

If for the last line of input $1 ~ /RESPN/, then the outer loop can't exit.

If for the last line of input $1 !~ /RESPN/, then the inner loop can't exit.

Catch 22.

One solution would be to restructure the inner loop to use while(getline) and use "break" to exit the loop when appropriate.

Rod Knowlton
IBM Certified Advanced Technical Expert pSeries and AIX 5L

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top