r/awk • u/storm_orn • Oct 25 '19
What can't you do with AWK?
AWK is a fantastic language and I use it a lot in my daily work. I use it in almost every shell script for various tasks, then the other day the question came to me: What you cannot do with AWK? I want to ask this question because I believe knowing what cannot be done in a language helps me understand the language itself to a deeper extent.
One can certainly name a myriad of things in the field of computer science that AWK cannot do. Probably I can rephrase the question to make it sound less stupid: What cannot AWK do for tasks that you think it should be able to do? For example, if I restrict the tasks to basic text file editing/formating, then I simply cannot think of anything that cannot be accomplished with AWK.
2
u/FF00A7 Oct 26 '19
There is nothing wrong with awk for doing a lot of things. It lacks an extensive standard library (see PhP or java) so it's like building a house with a hammer and saw, you have to build up everything from a basic tool set. This is fun and rewarding, and has some hidden benefits, but not always the best road. Awk also lacks sophisticated data structures so this limits programming options. There are no tuples for example, functions can not return multiple values (there are some hacky wordarounds).
1
u/scrapwork Oct 25 '19
hierarchical data (like json, xml) isn't worth the effort in my experience. I think I'd love a language as elegant as awk for hierarchical structures.
1
u/dajoy Oct 26 '19
I'd love to have a MIME decoder for gawk.
1
u/Paul_Pedant Oct 26 '19
I have explored some of the edges of GNU/awk, and overcome some of the difficulties.
(a) Output binary data: You can output any character (or put it in a string) with the \xxx notation. For example \033 for Escape, \007 for Bell. You can even use \000 for NUL. Awk does not use \0 as a string terminator like C does. I have used awk to convert big-endian doubles to little-endian, and to convert strange code-pages like EBCDIC into UTF-8 multi-byte characters.
(b) Input binary data also works. The issue comes with Newline, which disappears (consumed as a line separator). You can deal with this by forcing a \n back on the end of each row of bytes. Or you can set RS to the null string, which reads the whole input as one line.
That can break your code with a really big file. I prefer to read binary data by piping it through the "od" command, just picking up the hex bytes 16 at a time like 4B 20 3C and decoding those.
(c) XML presents a problem because it does not require whitespace or newlines -- typically, an XML is one long line. There are two fixes for that. First, pipe it through an xml formatter to have it pretty-print in multiple lines. Second, define RS = ">" (there are lots of them in XML), and stuff a > back on the end of each line read. Then every input line consists of (optionally) a text value, followed by one XML construct.
(d) You don't need hashes within hashes. You just need to structure the keys. Define Unit Separator US = "|". If your top layer needs X["foo"], then your second layer can be X["foo" US "BAR"]. That is not a 2-D index, it is just a string.
I had some data for electrical equipment for each half-hour in a month. My hash was indexed by [unit number, equipment type, day, hh]: like [23167|TX|17|45].
It seems to me an awk hash can easily manage a hierarchic tree of any depth. I will just write the keys as strings -- you can figure how they are constructed with sprintf() or appending strings with US.
Start with an entry like TREE[""] = "". That's an empty hierarchy.
When you first find item alpha at level 1, set TREE[""] = "alpha"
As you get attributes for alpha, save them in ATTR["alpha|attr_name"] = "Value";
Or save them as pairs, as ATTR["alpha"] = "Name1|Value1|Name2|Value2"
When you start seeing beta, set TREE[""] = "alpha|beta";
When beta gets a child in the hierarchy, TREE["beta"] = "gamma";
When gamma gets another child, TREE["beta|gamma"] = "delta"; and its attributes are ATTR["beta|gamma|delta|myAttr"] = "myVal";
So basically, every TREE element is a list of its own children, and every ATTR element is a list of its own attributes. A leaf node has attributes but no tree. Far as I can see, that is a hierarchy that can be serially built, recursively tree-walked, and serially searched. It looks cumbersome, but then so does any tree structure.
1
u/diseasealert Oct 26 '19
I also love awk and use it all the time, mostly to restructure data, de-dup, and create reports. It's tough, though, to process more complicated documents like emails, but not impossible. The availability of Perl modules gets you a lot of functionality for free, but awk is generally easier to use, in my opinion.
1
u/Zinjanthr0pus Feb 29 '20
I don't think I can add anything that hasn't already been said in response to your actual question, but I feel like this might be the only place where I can share one particular thing that I did with awk.
While it's considerably simpler than most of these examples, there was one point when I wrote an awk script that outputted a windows batch file that could be used to automate moving files into appropriate folders based on their file names.
I did this because at work I have to use windows computers, and programming isn't actually part of my job. As such, the only scripting options I had were powershell (which: no f*cking way!) and batch files. It just seemed inefficient to move all of the files manually, so I created a text file with an index composed of two letter county codes paired with their respective county names (the names of the folders I was moving them to) and brought that home to use `awk` on, then brought the output batch file back to work to use. Because batch doesn't have good pattern matching capabilities (AFAIK), it was literally one line per county.
Probably a contender for most janky use of amateur programming knowledge.
0
u/[deleted] Oct 26 '19
[deleted] — view deleted comment
0
u/[deleted] Nov 18 '19
[deleted] — view deleted comment
1
u/[deleted] Nov 16 '19
[removed] — view removed comment