Windows 中的 Grep 和 Awk 表达式错误中的无效字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4852270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Grep and Awk in Windows Invalid Char in Expression Error
提问by Nathan
I am new to grep and awk - using Windows 7 (I downloaded grep and awk for windows from GnuWin).
我是 grep 和 awk 的新手 - 使用 Windows 7(我从 GnuWin 下载了用于 Windows 的 grep 和 awk)。
I am have having trouble running this script:
我在运行此脚本时遇到问题:
grep -Fwf dictionary.txt frequency.txt | awk '{print "," }'
I get the error:
我收到错误:
awk: '{print
awk: ^ invalid char ''' in expression
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.
我相信这可能与必须在 Windows 中使用双引号有关,但我尝试了所有我能想到的组合,但仍然不起作用。
Can anyone help? Thanks
任何人都可以帮忙吗?谢谢
采纳答案by Benoit
Escaping command line items is always a pain on Windows. As a last resort you could probably use gawk -f
!
转义命令行项目在 Windows 上总是很痛苦。作为最后的手段,您可能可以使用gawk -f
!
So: your file script.awk
contains:
所以:您的文件script.awk
包含:
print ,
And you do grep -Fwf dictionary.txt frequency.txt | awk -f script.awk
你也是 grep -Fwf dictionary.txt frequency.txt | awk -f script.awk
回答by Just a learner
On Windows, you need to use double quotes to quote your awk commands. So
在 Windows 上,您需要使用双引号来引用 awk 命令。所以
grep -Fwf dictionary.txt frequency.txt | awk '{print "," }'
needs to be changed to
需要改为
grep -Fwf dictionary.txt frequency.txt | awk "{print "," }"
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:
但是请记住,您不能在双引号内使用双引号,您需要对它们进行转义。在 Windows 上,您不能简单地\用来转义它。您需要以下语法:
grep -Fwf dictionary.txt frequency.txt | awk "{print \"",\"" }"
Right, that's \""to represent "inside double quotes.
没错,就是用\""来表示"双引号内。
回答by ReluctantBIOSGuy
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!\"}"
您需要在 awk 脚本周围使用双引号,并使用旧的反斜杠转义打印语句中嵌入的引号: [g]awk "BEGIN {print \"Hello escape char!\"}"
回答by Alex Jelenic
Here is a short example which will accept an input.csv , then output new.csv
:
这是一个简短的示例,它将接受 input.csv ,然后输出new.csv
:
gawk < input.csv -F, "{print \"",\"" } ">new.csv
回答by Adrian J. Fabre
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}"
gawk "{print $2 """,""" $1}"
回答by Rainald62
Yousui said:
有穗说:
... you can't use double quotes inside double quotes, you need to escape them. On Windows you can't simply use \ to escape it.
...你不能在双引号内使用双引号,你需要转义它们。在 Windows 上,您不能简单地使用 \ 来转义它。
while ReluctantBIOSGuy has used just \" in his example.
而 ReluctantBIOSGuy 在他的例子中只使用了\"。
I tried both \"" and \", and both works for me (gawk under Windows XP both on the command line and in a batch file).
我尝试了 \"" 和 \",两者都对我有用(在 Windows XP 下在命令行和批处理文件中都呆了)。
Here is an example involving " in the output (a string literal in c code):
这是一个在输出中涉及“的示例(c 代码中的字符串文字):
FCIV\fciv -add ..-Sources -type *.h -type *.c -bp ..-Sources | find /V "//" | sort /+33 > listof.md5
FCIV\fciv -add listof.md5 | find /V "//" | gawk "{print \"static const char md5[] = \\"\" \"\\";\"}" > ..-Sources\generated\md5.c
回答by Gavin
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.
多年来,我一直在努力让 AWK 在 Windows 下运行。引用和路径分隔符存在问题。我的最终解决方案是“让 AWK 自由飞翔”,即从命令行中解放出来。我知道它是作为 unix 风格命令行 juju 的粘合剂开发的,但我只是想将它用作脚本语言。
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:
我所有的 AWK 脚本都包含目标列表和定义的输出文件。它们可以通过双击关联的 DOS 批处理文件来运行:
: AWK.BAT - place in the same directory as GAWK
@echo off
:Check %1 in not null
If [%1]==[] (
cls
Echo No parameters passed
goto End
)
: Change to the parameter file location
cd /D "%~dp1"
: Set PrintFile - this will be the name of the script (not the target file) with ".out"
Set PrintFile=%~nx1.out
:Run AWK
: -v PrintFile to allow renaming of output file
: -f ScriptFile.awk the program
: > Redirects output to a known destination
cls
P:\MyPrograms\EDITORS\Addins\gawk\gawk.exe -v PrintFile=%PrintFile% -f %* >%PrintFile%
:End
pause
An example of my AWK scripts is presented below (extract all lines with ::tab and print them):
我的 AWK 脚本示例如下(使用 ::tab 提取所有行并打印它们):
# AWK Template
BEGIN{
## Hard Code Target Files - Unix paths with / separators ##
# Realtive paths from the location of ScriptFileName.awk
# These will be added to the end of the ARG array - after any command line target files
AddTarget("../APEdit.ahk")
## Hard Code Output Files - WinDos paths with \ separators ##
# Realtive paths from the location of ScriptFileName.awk
# Default is ScriptFileName.awk.out passed in as a variable called PrintFile
# PrintFile will be copied to OutputFile after processing using the END section
OutputFile = "Keys.txt"
# Set input record sep and field sep
RS="\n"
FS=" "
# Set output RS and FS
ORS="\n"
OFS=" "
# Write a header
print "Key assignments from the source code"
print " "
}
## MIDDLE - Once per matching record! ##
# Find autohotkey key definitions
/::\t/ {
print ##代码##
}
END{
## Rename output files
if (OutputFile) {
system("Copy /Y " PrintFile " " OutputFile)
}
}
## Functions ##
function AddTarget(FN){
# Need to check file exists
if (FileExists(FN)){
ARGV[ARGC] = FN
ARGC ++
}
}
function FileExists(FN) {
if ((getline < FN) > 0) {
close(FN);
return 1
} else {
print "Target file not found " FN > "error.awk.txt"
return ""
}
}
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.
您可以看到这定义了脚本中的输入目标并定义了脚本中的最终输出目标。它使用临时“.out”文件来避免大量打印重定向,将文件复制到脚本的 END 部分中的所需输出。
I have associated AWK files with this batch file and added an option in my editor to send AWK files to the batch file.
我已将 AWK 文件与此批处理文件相关联,并在我的编辑器中添加了一个选项以将 AWK 文件发送到批处理文件。
Kind Regards
亲切的问候