简体   繁体   English

awk 无法在 windows 上打印字符串

[英]awk cannot print strings on windows

Is there something I'm doing wrong?有什么我做错了吗?

this gives an output:这给出了 output:

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

this does not:这不会:

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

this is gnu awk on windows.这是 windows 上的 gnu awk。

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. ) 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?我在堆栈溢出上找不到有关此特定问题的任何问题,但在 windows 与 linux 上处理字符串的方式可能存在简单的区别?

See print documentation , also see PC using documentation , which treats end-of-line but doesn't mention string differences.请参阅打印文档,另请参阅PC using documentation ,它处理行尾但未提及字符串差异。

You need to wrap the awk command with double quotes , and escape all the in-command double quotes with ^" outside of the quotes:您需要用双引号包裹awk命令,并在引号使用^"转义所有命令内双引号:

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:看截图:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM