Windows 中的 Grep 和 Awk 表达式中的无效字符错误

发布于 2024-10-15 17:48:39 字数 344 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

爱冒险 2024-10-22 17:48:39

在 Windows 上,您需要使用双引号来引用 awk 命令。所以

grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'

需要改为

grep -Fwf dictionary.txt frequency.txt | awk "{print $2 "," $1}"

但是记住,你不能在双引号内使用双引号,你需要转义它们。在 Windows 上,您不能简单地使用 \ 来转义它。您需要以下语法:

grep -Fwf dictionary.txt frequency.txt | awk "{print $2 \"",\"" $1}"

对,即 \"" 表示双引号内的 "

On Windows, you need to use double quotes to quote your awk commands. So

grep -Fwf dictionary.txt frequency.txt | awk '{print $2 "," $1}'

needs to be changed to

grep -Fwf dictionary.txt frequency.txt | awk "{print $2 "," $1}"

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:

grep -Fwf dictionary.txt frequency.txt | awk "{print $2 \"",\"" $1}"

Right, that's \"" to represent " inside double quotes.

尴尬癌患者 2024-10-22 17:48:39

在 Windows 上转义命令行项始终是一件痛苦的事情。作为最后的手段,你可能可以使用gawk -f

所以:您的文件 script.awk 包含:

print $2,$1

并且您执行 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:

print $2,$1

And you do grep -Fwf dictionary.txt frequency.txt | awk -f script.awk

生生漫 2024-10-22 17:48:39

您需要在 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!\"}"

断念 2024-10-22 17:48:39

这是一个简短的示例,它将接受 input.csv ,然后输出 new.csv :

gawk < input.csv -F, "{print $1 \"",\"" $5} ">new.csv

Here is a short example which will accept an input.csv , then output new.csv:

gawk < input.csv -F, "{print $1 \"",\"" $5} ">new.csv
谁的年少不轻狂 2024-10-22 17:48:39

由于括号必须位于双引号内,因此括号表达式内需要三组双引号。

例如:

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}"

爱格式化 2024-10-22 17:48:39

多年来我一直在努力让 AWK 在 Windows 下运行。引用和路径分隔符存在问题。我最终的解决方案是“让AWK自由飞翔”,即脱离命令行。我知道它是作为 unix 风格命令行 juju 的粘合剂而开发的,但我只是想将它用作脚本语言。

我的所有 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

下面是我的 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 $0
}

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 ""
    }
}

您可以看到这定义了脚本中的输入目标并定义了脚本中的最终输出目标。它使用临时“.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:

: 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 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 $0
}

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.

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

兮子 2024-10-22 17:48:39

尤随说道:

...你不能在双引号内使用双引号,你需要转义它们。在 Windows 上,您不能简单地使用 \ 来转义它。

而 ReluctantBIOSGuy 在他的示例中仅使用了 \"。

我尝试了 \"" 和 \",并且两者都对我有用(在 Windows XP 下在命令行和批处理文件中 gawk)。

下面是一个在输出中涉及 " 的示例(C 代码中的字符串文字):

FCIV\fciv -add ..\07-Sources -type *.h -type *.c -bp ..\07-Sources | find /V "//" | sort /+33 > listof.md5
FCIV\fciv -add listof.md5 | find /V "//" | gawk "{print \"static const char md5[] = \\\"\" $1 \"\\\";\"}" > ..\07-Sources\generated\md5.c

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.

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):

FCIV\fciv -add ..\07-Sources -type *.h -type *.c -bp ..\07-Sources | find /V "//" | sort /+33 > listof.md5
FCIV\fciv -add listof.md5 | find /V "//" | gawk "{print \"static const char md5[] = \\\"\" $1 \"\\\";\"}" > ..\07-Sources\generated\md5.c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文