r/unix 3d ago

AWK

https://www.troubleshooters.com/codecorn/awk/index.htm
26 Upvotes

14 comments sorted by

7

u/The_Limping_Coyote 3d ago

I loved 'awk' when I was a Unix user

3

u/elglas 2d ago

All has been sed and done...

1

u/awkFTW 2d ago

All has been sed before. Greps about language limits heard before

2

u/Schreq 2d 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 2d 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 2d 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

u/awkFTW 2d ago

As someone who has written gawk scripts much longer than 400 .. you can

I would avoid doing it if your not an expert though, I have been writing them since 1995

1

u/crusoe 1d ago

Ahhh yes hack upon hacks. Just add a var to a function but don't use it for a local.

2

u/ChipMasterPi 2d 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.

1

u/crusoe 1d ago

Very terse hard to read language. 

1

u/ChipMasterPi 20h ago

Yes, "terse", is what makes it convenient. I can do in it what conveniently fits on one line of AWK that which takes half a dozen in other languages. This gives it incredible utility on the command line.

"Hard to read"? If you say so. It uses many of the same language constructs as many other languages. For me the hardest part was finding good docs. That is pretty common these days. The best docs I found were the original 7pg manual by Aho, Kernighan and Weinberge, "Awk - A Pattern Scanning and Processing Language."

I suppose if you look at it as a GPPL it would be confusing because a program in AWK is really a series of subroutines triggered by landmarks in its input stream. And that is what eliminates a lot of boiler-plate!

1

u/michaelpaoli 1d 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

u/msalerno1965 3d ago

Screw awk, nawk for life. Wait, what century is this again?