What does $0=$2 in awk do?

by sorcercodeon 9/25/2022, 5:45 AMwith 85 comments

by asicspon 9/26/2022, 3:19 AM

Here are some more learning resources:

* https://backreference.org/2010/02/10/idiomatic-awk/ — how to write more idiomatic (and usually shorter and more efficient) awk programs

* https://learnbyexample.github.io/learn_gnuawk/preface.html — my ebook on GNU awk one-liners, plenty of examples and exercises

* https://www.grymoire.com/Unix/Awk.html — covers information about different `awk` versions as well

* https://earthly.dev/blog/awk-examples/ — start with short Awk one-liners and build towards a simple program to process book reviews

by bspammeron 9/25/2022, 9:34 PM

Elegant, but not something you should ever use outside of ad-hoc situations.

I think this is more comprehensible, and is also more robust because it actually specifies the field you're looking for rather than just any line with quotes:

gawk -F'"' '/^ name:/ {print $2}' appVersion.gradle

(hacker news is clobbering the spaces after ^)

by dietricheppon 9/25/2022, 9:26 PM

Awk is such a weird tool--it's powerful and so few people know how to leverage it.

Yesterday, someone in chat wanted to extract special comments from their source code and turn them into a script for GDB to run. That way they could set a break point like this:

  void func(void) {
    //d break
  }
They had a working script, but it was slow, and I felt like most of the heavy lifting could be done with a short Awk command:

  awk -F'//d[[:space:]]+' \
    'NF > 1 {print FILENAME ":" FNR " " $2}' \
    source/*.c
This one command find all of those special comments in all of your source files. For example, it might print out something like:

  source/main.c:105 break
  source/lib.c:23 break
The idea of using //d[[:space:]]+ as the field separator was not obvious, like many Awk tricks are to people who don't use Awk often (that includes me).

(One of the other cases I've heard for using Awk is for deploying scripts in environments where you're not permitted to install new programs or do shell scripting, but somehow an Awk script is excepted from the rules.)

by zwkrton 9/25/2022, 9:31 PM

Awk, like vim, is a tool I love dearly that I absolutely would never recommend someone else learn. It’s like a form of mental illness but it’s so lodged in my brain and I’ll give it up when they put me in the grave.

by scubboon 9/25/2022, 9:45 PM

Off-topic, but I _love_ the "sidenote" format for footnotes. I've been meaning to implement that in my own blog for a while, now. I'll check out the source for inspiration.

by xorciston 9/26/2022, 10:58 AM

Do not do this in awk when the same in sed would be easier to read.

But also do not match this data format in the first quote. Match "name:" instead if that is what you mean. This makes the intention clear.

For matching text, just use grep is that is what most people would expect:

  grep -m 1 "name:" appVersion.gradle | grep -o "[0-9.]"
You can shorten it to one regexp if you use an extended format that can handle backrefs if the context would make that clearer.

by jrm4on 9/25/2022, 11:05 PM

Yeah, this just proves to me that the intuitive shell tools are way better. I'll keep all the heads, tails, cuts, and trs, etc.

by pgporadaon 9/25/2022, 9:01 PM

Wonderful. My old license plate was awk sed.

by lugaoon 9/26/2022, 11:41 AM

As others here pointed out: this is bad awk.

I think good awk scripts lie somewhere between "This is atrociously overfit to this file" and "this is so general you should have done it in python/perl/etc".

Of course there are legitimate uses for both super-specific and super-general awk scripts, but finding the right compromise is what makes a good awk script. You want your script to be concise yet robust to changes in the input files. Also, readability and maintainability are really important if you plan to add it to an important script.

Short awk != good awk.

by FPGAhackeron 9/26/2022, 10:33 AM

> This is really such a gorgeous piece of code. Clever and poetic.

I suppose that is subjective, but I think this is terrible code.

The fact that someone had to write a lengthy blogpost to figure out what it was doing should be an obvious warning against it.

Write code for readability / comprehensibility.

by ketanmaheshwarion 9/26/2022, 12:55 PM

Too much text to explain what could have been explained clearly in 3-4 sentences. Making simple things complicated and then writing a long essay with dramatic phrases is a disservice to awk.

by pmarreckon 9/26/2022, 7:50 PM

Awk strikes me as a marvelous text parsing tool that too few understand.

by dark-staron 9/26/2022, 11:27 AM

$0=$2 sounds a lot like my wallet recently...

by DesiLurkeron 9/26/2022, 5:11 AM

takes 2 dollars and spends it!