Answered OT - an awk problem.

ron

Member
I hope there is a Unix guru out there who can help me.

I've written a number of awk scripts before OK, but I'm stuck on a simple but aggravating issue.

I've cut the code down to just a small test snippet:
Code:
#!/bin/ksh93
 
cat /tmp/testfile | awk 'BEGIN { count = 0; }
 
  /\(3777\)/ { print substr( $0, 66, 20); count++ };
 
  count > 7 { exit };
'

The testfile is a copy of an OE10 db.lg file (not that that's important). The above code works - and produces 8 lines of output, as it should. However, if I change the syntax of the last statement to make it an if statement - I get a syntax error:

Code:
#!/bin/ksh93
 
cat /tmp/testfile | awk 'BEGIN { count = 0; }
 
  /\(3777\)/ { print substr( $0, 66, 20); count++ };
 
  if ( count > 7 ) { exit };
'

Code:
/tmp > t
syntax error The source line is 5.
The error context is
                  >>>  if <<<  ( count > 7 ) { exit };
awk: Quitting
The source line is 5.

Can someone explain what I'm doing wrong? I've used if statements before without a problem - but now ... I'm stuck.

Ron.
 

ron

Member
I hope there is a Unix guru out there who can help me.

I've written a number of awk scripts before OK, but I'm stuck on a simple but aggravating issue.

I've cut the code down to just a small test snippet:
Code:
#!/bin/ksh93
 
cat /tmp/testfile | awk 'BEGIN { count = 0; }
 
  /\(3777\)/ { print substr( $0, 66, 20); count++ };
 
  count > 7 { exit };
'

The testfile is a copy of an OE10 db.lg file (not that that's important). The above code works - and produces 8 lines of output, as it should. However, if I change the syntax of the last statement to make it an if statement - I get a syntax error:

Code:
#!/bin/ksh93
 
cat /tmp/testfile | awk 'BEGIN { count = 0; }
 
  /\(3777\)/ { print substr( $0, 66, 20); count++ };
 
  if ( count > 7 ) { exit };
'

Code:
/tmp > t
syntax error The source line is 5.
The error context is
                  >>>  if <<<  ( count > 7 ) { exit };
awk: Quitting
The source line is 5.

Can someone explain what I'm doing wrong? I've used if statements before without a problem - but now ... I'm stuck.

Ron.
 

ron

Member
I have solved the problem. Apparently an if statement is "invalid" unless it is inside a block. I enclosed everything inside {} braces - and now awk is happy. Strange limitation.
Ron.
 

ron

Member
I may not know the particular meaning you have in mind, Tom! But if it is oriented towards something like "awkward" ... then I sure wouldn't mount an argument against it! :p

Nevertheless - there are a few things it is useful for.
 
Top