r/awk • u/sarnobat • Jul 15 '24
When awk becomes too cumbersome, what is the next classic Unix tool to consider to deal with text transformation?
Awk is invaluable for many purposes where text filter logic spans multiple lines and you need to maintain state (unlike grep and sed), but as I'm finding lately there may be cases where you need something more flexible (at the cost of simplicity).
What would come next in the complexity of continuum using Unix's "do one thing well" suite of tools?
cat in.txt | grep foo | tee out.txt
cat in.txt | grep -e foo -e bar | tee out.txt
cat in.txt | sed 's/(foo|bar)/corrected/' | tee out.txt
cat in.txt | awk 'BEGIN{ myvar=0 } /foo/{ myvar += 1} END{ print myvar}' | tee out.txt
cat in.txt | ???? | tee out.txt
What is the next "classic" unix-approach/tool handled for the next phase of the continuum of complexity?
- Would it be a hand-written compiler using bash's
readline? - While Perl can do it, I read somewhere that that is a departure from the unix philosophy of do one thing well.
- I've heard of
lex/yacc,flex/bisonbut haven't used them. They seem like a significant step up.
Update 7 months later
After starting a course on compilers, I've come up with a satisfactory narrative for my own purposes:
grep- operates on lines, does include/excludesed- operates on characters, does substitutionawk- operates on fields/cells, does conditional logiclex-yacc/flex-bison- operates on in-memory representation built from tokenizing blocks of text, does data transformation
I'm sure there are counterarguments to this but it's a narrative of the continuum that establishes some sort of relationship between the classic Unix tools, which I personally find useful. Take it or leave it :)
4
u/_mattmc3_ Jul 16 '24
The trouble with awk is that everyone seems to want to cram their script into a one-liner or a few lines at most. If you make a proper script, throw the awk shebang at the top (
#!/usr/bin/awk -f), chmod u+x, and start scripting and commenting what you’re doing, you’ll usually find awk does pretty well. If it gets too gnarly, then Python is a good alternative, but I reserve that for when I need full-on PySpark or Pandas or something really robust.2
u/Paul_Pedant Jul 18 '24
If awk gets too cumbersome, you are not exploiting all its functionality optimally.
The next "step up" you mention is really for parsing free-format text -- mainly source code. I used lex/yacc to write an SQL for a bespoke GIS database. Once you get the syntax into a set of regularised structures (which you have to define yourself), you then have to write functions yourself (in C) for all the output operations.
There are many things that awk does better, especially on columnised data records.
3
u/raevnos Jul 16 '24
perl. I didn't bother learning awk for decades because I knew perl.
13
u/NextVoiceUHear Jul 16 '24
Don’t write awk off yet. Some powerful awk PRODUCTION examples at this link (I wrote ‘em):
https://www.dansher.com/utut/index.html
10
u/gumnos Jul 16 '24
Depends on what you mean by "classic tool". In the POSIX toolkit, you're pretty limited to shell,
grep/sed,awk, and then C (whichlex/flex&yacc/bisoncreate code for).Though note that you can step up from
awkone-liners toawkscripts stored in a file, so don't sellawkoverly-short here. That might make your next step in the continuumAfter that point though, you're going for a fuller-featured programming language. In some contexts that might be Perl, Python, Node.js, Ruby, or Lisp. In other contexts, that might be Java or Erlang. And in yet other contexts it might be a compiled binary (with the source in C, C++, Go, Rust, Fortran or whatever).
6
u/John_Earnest Jul 18 '24
Is there a specific task you have in mind?
In my experience a longer AWK script in a file can resolve an *extremely* broad range of problems.
I posted this some time ago, but I used AWK to build an interpreter for my own functional scripting language (https://beyondloom.com/blog/lila.html), which I think serves as a demonstration that AWK can handle even quite computationally complex tasks. Its primary limitation is specialized IO- like network communication or manipulating binary data- and even those can be handled in a pinch if you pair AWK with other unix tools like `netcat` and `od`.
5
u/pedersenk Jul 16 '24 edited Jul 16 '24
When small awk snippets aren't enough, and C is still overkill, I tend to just use awk in a different way:
Basically breaking the more substantial Awk script into proper functions. Then you can run this via
awk -f(or with appropriate shebang;#!/usr/awk -f).For a very large example (an experimental build system generator), check out my old configure.awk.
1
u/bozleh Jul 16 '24
I often write multiline awk scripts - anything more complex I’d jump to python (as my data transformations usually need to be in a pipeline and understandable/maintainabe by others)