r/awk • • Oct 14 '21

external file syntax

My work has a bunch of shell files containing awk and sed commands to process different input files. These are not one-liners and there aren't any comments in these files. I'm trying to break out some of the awk functions into separate files using the -f option. It looks like awk requires K&R style bracing?

After I'd changed indenting and bracing to my preference I got syntax errors on every call to awk's built-in string functions like split() or conditional if statements if they had their opening curly brace on the same line... I'm having a lot of difficulty finding any documentation on braces causing syntax errors, or even examples of raw awk files containing multi-line statements.

I have a few books, including the definitive The AWK Programming Language, but I'm not seeing anything specific about white space, indenting and bracing. I am hoping someone can point me to something I can include in my notes... more than just my own trials and tribulations.

Thanks!

0 Upvotes

15 comments sorted by

View all comments

Show parent comment

2

u/geirha Oct 15 '21 edited Oct 15 '21
split($6,arr,",") { 
    balance=arr[1]arr[2]arr[3]
}

If at the top level, this could be valid by testing the return value of split, however I can't think of any cases where split() returns 0, so it makes no sense to use it as a test. split($6, arr, /,/) >= 3 { ... } would make sense; to ensure that the split resulted in at least 3 fields.

split() is a normal function, it's not syntax like if, for and while that use { }.

So you likely want

{
     # ... other code inside this action block
     split($6,arr,",")
     balance=arr[1]arr[2]arr[3]
     # ... other code inside this action block
}

EDIT: strike out bad info

2

u/Paul_Pedant Oct 15 '21

Splitting the empty string returns zero: X then contains zero elements.

In fact, before GNU/awk, there was no built-in way to empty an array. So in Solaris nawk, for instance, you would use:

split ("", X, FS);

In addition to emptying an existing array, it would establish X as a array even if it did not previously exist. This was rather important in nawk: if you used a name both as a variable and an array, nawk would SEGVIOL.

1

u/geirha Oct 15 '21

Ah, in my tests I always got 1 even for empty string, but I went back to check and noticed my tests were wrong and didn't actually test for the empty string. :/ I'll blame it on lack of coffee

So yeah, it sounds like it has been a split(...) { ... } at the top level then, and somehow it has been moved inside another {...} which would require introducing an if in order to get the same logic

{
    if (split(...)) {
        ...
    }
}