[Solved] looping over AWK commands doesn’t work [duplicate]


Because your awk program is in single quotes, there will not be any shell variable expansion. In this example:

awk 'tolower($0)~/^alphabet/{print}' titles-sorted.txt > titles-links/^alphabet.txt

…you are looking for the lines that begin with the literal string alphabet.

This would work:

awk "tolower(\$0)~/^$alphabet/{print}" titles-sorted.txt > titles-links/$alphabet.txt

Note several points:

  • We are using double quotes, which does not inhibit shell variable expansion.
  • We need to escape the $ in $0, otherwise the shell would expand that.
  • We need to replace alphabet with $alphabet, because that’s how you refer to shell variables.
  • We need to replace ^alphabet with $alphabet in the filename passed to >.

You could also transform the shell variable into an awk variable with -v, and do this:

for alphabet in {a..z} ; do
    awk -valphabet=$alphabet 'tolower($0)~"^"alphabet {print}' /usr/share/dict/words > words-$alphabet.txt
done

solved looping over AWK commands doesn’t work [duplicate]