Tuesday, October 26, 2021

[SOLVED] What are $0 and $1 (dollar sign 0 and 1) in an awk script?

Issue

For example in this awk tutorial there are three examples:

  1. tolower($1) ~ /mary/ { print "CI Record: " $0; }
  2. $0 !~ /Mary/ { print "Not Mary: " $0; }
  3. $1 == "Mary" { print "Mary Record: " $0; }

Solution

Actually example # 2 is using a regex because of this syntax

/regex/

Which means in your example that if literal text Mary isn't found anywhere in whole line ($0) then execute awk code.

Whereas $1 == "Mary" is doing direct comparison between literal text Mary and field # 1 ($1).

Finally tolower($1) ~ /mary/ is again using ignre-case regex match on field # 1 and this means if $1 has text mary (ignore-case) then execute rest of the awk code.



Answered By - anubhava