Does anyone have concrete, practical examples of use cases where awk made your life much easier?
Does anyone have concrete, practical examples of use cases where awk made your life much easier?
For example, recently I needed to print out certain fields of an output, but only for a given subset. So I used Awk to create a simple state machine (enable when I see the start of the subset, disable at the end), and print the fields of interest.
Also I use awk often in scripts together with fzf to interactively switch contexts e.g. in cloud provider CLIs.
Awk has a hash like perl which is very efficient. I use an awk expression to print uniq as they come in counted through the hash insert on new instead of uniq which prints at end.
Awk count unique over 300,000,000 ips was as fast as perl and python and smaller memory footprint
I don't know what LWSP gobbling is, though. Google didn't help.
[1] https://stackoverflow.com/questions/21072713/what-exactly-is...
With the parent example it means that column #2 could have any of number whitespaces between it and column #1.
That's like answering "why use Haskell?" with "Because its monads are monoids in the category of endofunctors"
If argument expansion is required, consider using read. That way the argument is given a name. Use "read a b c < x" instead of "b=$(cat x | awk '{ print $2 }')".
Just remember to pipe to read with care, as the right hand part of a pipe is a subshell in which variables are local. So "read a b c < <(echo 1 2 3)" works, "echo 1 2 3 | read a b c" doesn't.
Another way to expand arguments is to simply define a shell function and use $2. Something like "process_line() { echo $2 }".
When you have to reach for something like awk, your script would probably improve by being mostly awk. In which case most people are probably looking at perl or python anyway. Even if awk is a nice language, the arrival of perl mostly killed it. It is not wrong to say perl was the next version of awk.
whereas, tr will still leave a trailing/leading space
for example:
$ echo ' a b c ' | tr -s ' ' | cut -d ' ' -f2
a
$ echo ' a b c ' | awk '{print $2}'
b
and by default awk splits on space/tab/newlines, whereas in cut example above, you get only space as delimitercut has its uses and will be faster than awk, but it depends on the problem being solved
It possible to use `xargs -L 1` to trim the separators, but then you would also add `findutils` as deps.
I just wanted to point out that keeping the dependencies of scripts in mind when programming is also important.
e.g. a regex or even a shell-style pattern match would be very useful. `cut -d ' +' -f2` would get rid of the need for 'tr' in your example.
Even just allowing >1 char delimiters would be helpful. A text list like 'foo, bar, foobar' is too much for cut, but it would be fine if cut accepted parameters like `cut -d ', '`
https://www.bignerdranch.com/blog/a-crash-course-in-awk/
A tl;dr for the following tl;dr: it's great for quick-and-dirty processing of logs.
The tl;dr is that AWK is simple language that lets you use a line-oriented event driven programming model to process text. This abstraction is simple enough to be readable and maintainable, but powerful enough to parse and process text with line-oriented structure.
The example linked elsewhere is worth studying, because it truly is one of the prettiest pieces of programming I've seen in a long while:
If you don't know anything about AWK, here's all you need to know to grok this:
1. An AWK program is a list of [event] { code } pairs. All pairs are run in sequence on each line of input; if the "event" evaluates to true or is a matching regex, the matching code runs.
2. { code } on its own runs unconditionally.
3. The input is automatically whitespace delimited into fields; $1 refers to field 1, $2 to field 2, etc
4. NF is a special value that yields the number of fields
5. Assigning to fields replaces the text in those fields.
6. ($1+0) != 0 implicitly converts $1 to an integer; if it fails, the value is 0.
There is a lot of implicit loose typing in the language, and usage of undefined variables is idiomatic. Functions aren't easy to use, either. So it's not well-suited for programming in the large, or even the medium. But for the scale of programming seen in that link, it's truly a wonderful and simple power tool.
I'd normally write this sort of thing in python. The awk program is usually smaller than a comparable python program, but for me the main selling point is that awk programs a more "UNIX-pipe-native" than python programs are.
At some point, I'll probably rewrite these programs in a more "serious" programming language, once the file sizes get too big or something, but for now they're working great and are easy to extend.
FWIW, you can get around it. I just took a peek because I couldn't remember how to do it off the top of my head.
It looks like I had to use
gawk -F ',' -v FPAT='([^,]+)|("[^"/]+")'
to get the behavior you're looking for. Seems like I nabbed it from https://www.gnu.org/software/gawk/manual/html_node/Splitting...Agreed that this is the sane point at which to pull out python.
The risk of that seems to be if CSV allows embedded escaped quotes in a quoted string. Does it? I don't know. And CSV is pretty loosely defined. For most people it's probably "whatever Excel emits or ingests".
And I think that's why we're on the same page about pulling out python and using a module where somebody has explored what the corner cases are and dealt with them for us already.
In my experience, each implementation is more or less unique. Even if you allow escaping, different formats permit different methods of escaping and you might have to account for each! shudders
It’s much easier to support that in python than it is in awk with regexp. The awk route will eventually make you a regexp wizard, though, which confers it’s own benefits. :)
* https://blog.jpalardy.com/posts/why-learn-awk/ (also discussed on HN: https://news.ycombinator.com/item?id=22108680)
* https://adamdrake.com/command-line-tools-can-be-235x-faster-...
if it becomes lengthy (and again depends on features required), then Python is likely to have inbuilt/3rd-party libraries to make it easier to write and maintain
if it is a question of constructing command line one-liners and using it as a part of other cli tools, then awk wins easily
for example:
awk -F'\\W+' -v OFS=, '{print $NF, $2}' input.txt
prints last column and second column, where non-word characters form the field separator and comma is used as output field separatorPlus I think semantic indentation is not sane language design.
Plus you can pipe it to another command with ease.