Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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; }
like image 313
0x90 Avatar asked Mar 26 '13 20:03

0x90


People also ask

What does dollar sign mean in awk?

You use a dollar sign (' $ ') to refer to a field in an awk program, followed by the number of the field you want. Thus, $1 refers to the first field, $2 to the second, and so on. (Unlike in the Unix shells, the field numbers are not limited to single digits.

What does $1 $2 indicate in awk file?

The variable $1 represents the contents of field 1 which in Figure 2 would be "-rwxr-xr-x." $2 represents field 2 which is "1" in Figure 2 and so on. The awk variables $1 or $2 through $nn represent the fields of each record and should not be confused with shell variables that use the same style of names.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

What is awk '{ print $4 }'?

'{print $4}' means print the fourth field (the fields being separated by : ).


1 Answers

In awk, $0 is the whole line of arguments, whereas $1 is just the first argument in a list of arguments separated by spaces. So if I put "Mary had a little lamb" through awk, $1 is "Mary", but $0 is "Mary had a little lamb". The second line is trying to find the substring "Mary" in the whole line given to awk.

like image 183
Advael Avatar answered Oct 11 '22 11:10

Advael