I’m trying to learn to use awk but it’s not behaving how I expect. Here’s my trouble:
$ echo "Hello brave new world" | awk "{print $1}"
Hello brave new world
I expected to see “Hello”, as this is the first field. Why don’t the spaces count as field delimiters?
It’s because Bash is interpreting
$1as referring to the first shell argument, so it replaces it with its value. Since, in your case, that parameter is unset,$1just gets replaced with the empty string; so your AWK program is actually just{print }, which prints the whole line.To prevent Bash from doing this, wrap your AWK program in single-quotes instead of double-quotes:
or