Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 706
mangelmentmanager Avatar asked Jun 09 '26 16:06

mangelmentmanager


1 Answers

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

like image 148
Wiktor Stribiżew Avatar answered Jun 12 '26 10:06

Wiktor Stribiżew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!