4 ms·
awk is one of those underrated tools I always wanted to learn. I still remember when I started working as a sysadmin at 19, the greybeard UNIX guy taught me ho
by 1_player 5y ago
awk is one of those underrated tools I always wanted to learn.
I still remember when I started working as a sysadmin at 19, the greybeard UNIX guy taught me how to vim, and he told me awk was as important as knowing vim, pointing to some huge AWK manual he had on the shelf, one of those with the animal in the cover.
This was 15 years ago, I know vim, but awk still eludes me.
- asicsp 5y agoI wrote a book on GNU awk one-liners with hundreds of examples and exercises [0]. Free to read online. For a quick introduction, see [1] [2] [0] https://github.com/learnbyexample/learn_gnuawk https://github.com/learnbyexample/learn_gnuawk [1] https://backreference.org/2010/02/10/idiomatic-awk/ https://backreference.org/2010/02/10/idiomatic-awk/ [2] https://earthly.dev/blog/awk-examples/ https://earthly.dev/blog/awk-examples/
- smcameron 5y agoIf you know C and regex, that's 85% to 95% of awk. The basic layout of an awk program is a sequence of regex/action pairs, where the regexes are between slashes, and the actions are between curly braces. (There's also the special regex values of BEGIN and END, executed prior to the first line of input and after all input is consumed, respectively). The action part has a syntax very much like C, with the special values $n (where n is an integer) representing field number n for a any input line. There are a bunch of special functions it defines that you can call, like substr(), or split(), and good old printf. Awk processes the input stream one line at a time, matching each line against the series of regexes, and if it matches a regex, it executes the action paired with that regex, with all the $n variables filled in with field values from that input line. And that's basically enough for 99% of what awk is used for on a day to day basis.
- barrkel 5y agoI wrote one script in awk and learned a lot: - I understood where a lot of the stuff in Perl (and, to a lesser degree, Ruby) came from, especially Perl's implicit loop mode. - I never wanted to write a program in awk again. Ruby (or whatever your preferred scripting language is) is better every time: awk is not much less work and Ruby generalizes more. Awk for scripting is a bit like shell for scripting. It's easy to extend a script into something which is uncomfortably expensive to rewrite when you inevitably want to do something it's not well suited to. Awk still has uses, but it's in things like portable shell scripts which need more text manipulation power.
- known 5y agohttps://archive.md/mmd80 https://archive.md/mmd80 is very good to master AWK
- yesenadam 5y agoRead The AWK Programming Language, a joy to read, one of the finest docs ever written, I reckon. You'll be up and running in minutes. https://archive.org/details/pdfy-MgN0H1joIoDVoIC7 https://archive.org/details/pdfy-MgN0H1joIoDVoIC7