For example in this awk
tutorial there are three examples:
tolower($1) ~ /mary/ { print "CI Record: " $0; }
$0 !~ /Mary/ { print "Not Mary: " $0; }
$1 == "Mary" { print "Mary Record: " $0; }
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.
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.
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.
'{print $4}' means print the fourth field (the fields being separated by : ).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With