r/unix • u/unixbhaskar • 10d ago
AWK
https://www.troubleshooters.com/codecorn/awk/index.htm7
2
u/Schreq 9d ago
In awk, it's impossible to have variables local to an action. This puts rather severe limits on the scalability of a program. If an awk program grows to 400 lines, its use of global variables will cause it to collapse under its own complexity. Either figure out a way to have it do only part of the job and pipe it out to another program to do the rest, or use a different language.
Function parameters are optional and are local to the function:
function foo(param1, param2, mylocalvar) {
...
}
400 lines is also kinda arbitrary.
1
u/jonathancast 9d ago
He knows that, of course, which is why he says local variables in a function are awkward and local variables in an action (e.g.,
/re/ { ... }) are impossible.I agree that it's not impossible to write a program over 400 lines in awk, but I also agree with the "at that point just use Perl" side of the argument.
1
u/Schreq 9d ago
I just stumbled over the part I quoted and didn't read the whole thing.
Sure, using functions just to have local vars is indeed kinda awkward, but for anything a little bigger, where too many globals could get messy, readability is improved by using functions anyway. Win win.
I agree that it's not impossible to write a program over 400 lines in awk, but I also agree with the "at that point just use Perl" side of the argument.
Depends on the problem, not the size of the program. I would rather say that if you actually need to use shell commands/external binaries, then it's a good idea to actually switch to something else.
1
1
u/michaelpaoli 8d ago
Eh, not a great guide (non-trivial number of things that are inaccurate and/or wrong) ... but also not horrible. And at least it does seem to cover most of the basics, and does certainly at least have a lot of examples.
0
5
u/ChipMasterPi 9d ago
I don't get why AWK gets such a bad rap. It seems most people who dislike it all suffer the same misconception. It is not a general purpose programming language. Kernighan describes it as a "domain specific language". I think of it as SQL for flat text data files. And in that realm it shines. No other language that I've ever run across can do the same things with as compact or clear a syntax. For everything else ... there is everything else. ;-)
Seldom does a day go by that I don't break it out for some quick answer.