What does effect does a trailing number have on the body of an awk script?
For awk
, 1 mean true, and when an expression is true, awk
print the current line by default.
Examples :
awk '1' /etc/passwd
will print the whole fileawk '0' /etc/passwd
will not print the whole file
If you're good in arithmetic (or you have imagination or programming skills), there's many tricks you can do with this behaviour.
You got your answer to your specific question BUT your posted script is unsafe. The first argument for printf is a format string, not data. You can sometimes use it to print data but only when it's hard-coded in your script, not when you're reading it from input. Look what happens if your input happens to contain a printf formatting string:
$ cat fileonetwo%sthreefour$ awk '/two/ { printf $1; next; } 1' fileoneawk: cmd. line:1: (FILENAME=file2 FNR=2) fatal: not enough arguments to satisfy format string `two%s' ^ ran out for this one
The correct way to write the script is:
$ awk '/two/ { printf "%s",$1; next; } 1' fileonetwo%sthreefour