Search code examples
windowspowershellawk

awk cannot print strings on windows


Is there something I'm doing wrong?

this gives an output:

echo "hello world" | awk '{ print $0 }'

this does not:

echo "" | awk '{ print "hello world" }' (neither does awk 'BEGIN{ print "hello world" }')

this is gnu awk on windows.

For the purpose of this question, all I'd like to do is print a string. This runs as expected on WSL/MSYS etc., but doesn't produce output when on windows (using powershell, with awk from git's linux tools and with awk installed from choco, same result. )

I couldn't find any questions regarding this specific issue on stack overflow, but perhaps there is a simple difference in how strings are handled on windows vs linux?

See print documentation, also see PC using documentation, which treats end-of-line but doesn't mention string differences.


Solution

  • You need to wrap the awk command with double quotes, and escape all the in-command double quotes with ^" outside of the quotes:

    echo "complicated output to be parsed with interesting word otherword" | awk "{ if ($0 ~ "^"".*sasquatch"^"") {print "^"" there is a sasquatch"^""} else if ($0 ~ "^"".*otherword"^"") {print "^""other word..."^""}}"
    

    See the screenshot:

    enter image description here