Windows 中的 Grep 和 Awk 表达式中的无效字符错误
我是 grep 和 awk 的新手 - 使用 Windows 7(我从 GnuWin 下载了适用于 Windows 的 grep 和 awk)。
我在运行此脚本时遇到问题:
grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'
我收到错误:
awk: '{打印
awk: ^ invalid char ''' in expression
我相信这可能与在 Windows 中使用双引号有关,但我尝试了我能想到的所有组合,但仍然不起作用。
有人可以帮忙吗? 谢谢
I am new to grep and awk - using Windows 7 (I downloaded grep and awk for windows from GnuWin).
I am have having trouble running this script:
grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'
I get the error:
awk: '{print
awk: ^ invalid char ''' in expression
I believe it might have something to do with having to use double quotes in Windows, but I tried all the combinations I can think of and still it doesn't work.
Can anyone help?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 Windows 上,您需要使用双引号来引用 awk 命令。所以
需要改为
但是记住,你不能在双引号内使用双引号,你需要转义它们。在 Windows 上,您不能简单地使用 \ 来转义它。您需要以下语法:
对,即 \"" 表示双引号内的 " 。
On Windows, you need to use double quotes to quote your awk commands. So
needs to be changed to
But remember, you can't use double quotes inside double quotes, you need to escape them. On Windows you can't simply use \ to escape it. You need the following syntax:
Right, that's \"" to represent " inside double quotes.
在 Windows 上转义命令行项始终是一件痛苦的事情。作为最后的手段,你可能可以使用
gawk -f
!所以:您的文件
script.awk
包含:并且您执行
grep -Fwfdictionary.txtFrequency.txt | awk -f 脚本.awk
Escaping command line items is always a pain on Windows. As a last resort you could probably use
gawk -f
!So: your file
script.awk
contains:And you do
grep -Fwf dictionary.txt frequency.txt | awk -f script.awk
您需要在 awk 脚本周围使用双引号,并使用旧的反斜杠转义 print 语句中嵌入的引号: [g]awk "BEGIN {print \"Hello escape char!\"}"
You need to use double quotes around your awk script and escape the embedded quotes in the print statement using a good old backslash: [g]awk "BEGIN {print \"Hello escape char!\"}"
这是一个简短的示例,它将接受 input.csv ,然后输出 new.csv :
Here is a short example which will accept an input.csv , then output
new.csv
:由于括号必须位于双引号内,因此括号表达式内需要三组双引号。
例如:
gawk "{print $2 """,""" $1}"
Since the brackets must be within double quotes, three sets of double quotes are required inside the bracketed expression.
For example:
gawk "{print $2 """,""" $1}"
多年来我一直在努力让 AWK 在 Windows 下运行。引用和路径分隔符存在问题。我最终的解决方案是“让AWK自由飞翔”,即脱离命令行。我知道它是作为 unix 风格命令行 juju 的粘合剂而开发的,但我只是想将它用作脚本语言。
我的所有 AWK 脚本都包含目标列表和定义的输出文件。它们可以通过双击关联的 DOS 批处理文件来运行:
下面是我的 AWK 脚本的示例(使用 ::tab 提取所有行并打印它们):
您可以看到这定义了脚本中的输入目标并定义了脚本中的最终输出目标。它使用临时“.out”文件来避免大量打印重定向,将文件复制到脚本的 END 部分中所需的输出。
我已将 AWK 文件与该批处理文件相关联,并在编辑器中添加了一个选项以将 AWK 文件发送到批处理文件。
亲切的问候
I have stuggled over the years to get AWK running under windows. There are problems with quoting and path delimiters. My final solution is to "let AWK fly free", that is free from the command line. I understand that it was developed as a glue for unix style command line juju, but I just wanted to use it as a scripting language.
All my AWK scripts contain a list of targets and a defined output file. They can be run by double clicking through an associated DOS batch file:
An example of my AWK scripts is presented below (extract all lines with ::tab and print them):
You can see this defines the input target within the script and defines the final output target within the script. It uses a temp ".out" file to avoid lots of print redirection, copying the file to the desired output in the END section of the script.
I have associated AWK files with this batch file and added an option in my editor to send AWK files to the batch file.
Kind Regards
尤随说道:
而 ReluctantBIOSGuy 在他的示例中仅使用了 \"。
我尝试了 \"" 和 \",并且两者都对我有用(在 Windows XP 下在命令行和批处理文件中 gawk)。
下面是一个在输出中涉及 " 的示例(C 代码中的字符串文字):
Yousui said:
while ReluctantBIOSGuy has used just \" in his example.
I tried both \"" and \", and both works for me (gawk under Windows XP both on the command line and in a batch file).
Here is an example involving " in the output (a string literal in c code):