awk 无法在 windows 上打印字符串
awk cannot print strings on windows
我做错了什么吗?
这给出了输出:
echo "hello world" | awk '{ print [=11=] }'
这不是:
echo "" | awk '{ print "hello world" }'
(awk 'BEGIN{ print "hello world" }'
)
这是 windows 上的 gnu awk。
为了这个问题的目的,我只想打印一个字符串。这在 WSL/MSYS 等上按预期运行,但在 windows 上不产生输出(使用 powershell,使用 git 的 linux 工具中的 awk 并安装了 awk来自 choco,结果相同。)
我找不到关于堆栈溢出的这个特定问题的任何问题,但在 windows 和 linux?
上处理字符串的方式可能有一个简单的区别
参见 print documentation, also see PC using documentation,其中处理行尾但未提及字符串差异。
您需要用 双引号 将 awk
命令括起来,并用 ^"
转义所有命令中的双引号引号外:
echo "complicated output to be parsed with interesting word otherword" | awk "{ if ([=10=] ~ "^"".*sasquatch"^"") {print "^"" there is a sasquatch"^""} else if ([=10=] ~ "^"".*otherword"^"") {print "^""other word..."^""}}"
见截图:
我做错了什么吗?
这给出了输出:
echo "hello world" | awk '{ print [=11=] }'
这不是:
echo "" | awk '{ print "hello world" }'
(awk 'BEGIN{ print "hello world" }'
)
这是 windows 上的 gnu awk。
为了这个问题的目的,我只想打印一个字符串。这在 WSL/MSYS 等上按预期运行,但在 windows 上不产生输出(使用 powershell,使用 git 的 linux 工具中的 awk 并安装了 awk来自 choco,结果相同。)
我找不到关于堆栈溢出的这个特定问题的任何问题,但在 windows 和 linux?
上处理字符串的方式可能有一个简单的区别参见 print documentation, also see PC using documentation,其中处理行尾但未提及字符串差异。
您需要用 双引号 将 awk
命令括起来,并用 ^"
转义所有命令中的双引号引号外:
echo "complicated output to be parsed with interesting word otherword" | awk "{ if ([=10=] ~ "^"".*sasquatch"^"") {print "^"" there is a sasquatch"^""} else if ([=10=] ~ "^"".*otherword"^"") {print "^""other word..."^""}}"
见截图: