5

I simply can't figure out how does this line of awk code work.

$$1~/^[^0-9]/ {print $$1;}
Kajquo
  • 53
  • 5

1 Answers1

6

The $ sign is an operator in awk to reference a field. Example:

$ echo "foo bar car" | awk '{print $2}'
bar

This prints bar, as bar is the content of the second field.

A double dollar sign is actually a double reference that will use the information of the first field reference to get to the other field reference. Example:

$ echo "foo bar car 1 2 3" | awk '{print $$5}'
bar
$ echo "foo bar car 1 2 3" | awk '{print $5}'
2

Here it prints bar as $5 is dereferenced as 2 and thus $$5 is equivalent to $2

kvantour
  • 25,269
  • 4
  • 47
  • 72
  • 3
    Ta make it simpler to read/understand, you could use parentheses like this: `awk '{print $($5)}'` It will expand the parentheses first, giving `2`, and then you get `$2` that gives `bar`. `echo "foo 3 car 1 2 3" | awk '{print $$$5}' => car` or `echo "foo 3 car 1 2 3" | awk '{print $($($5))}' = car` – Jotne Sep 19 '19 at 15:36