【问题标题】:AWK Escape double quotes in Winawk / GnuwinWinawk / Gnuwin中的AWK转义双引号
【发布时间】:2015-04-07 03:28:17
【问题描述】:

我不知道如何在 AWK 中搜索单词,Gnuwin。我在 AWK 中有以下内容:

..."{ if ($2==""""Word"""") print }"...

Word在我的文件中是用双引号写的,所以我必须用双引号搜索它。

我试过了: ..."{ if ($2=="""\"Word\"""") print }"... 以及许多其他方法,这些都不起作用。

有人知道这将如何工作吗?

提前谢谢你

【问题讨论】:

    标签: awk escaping double-quotes


    【解决方案1】:

    在 Windows shell 中,如果您在命令行上输入脚本,则必须使用反斜杠转义脚本内的双引号。而且,那些双引号,如果它们是某个字符串的一部分,由于 AWK 的规则,必须用反斜杠转义,并且由于该反斜杠在字符串内,它也必须用反斜杠转义:

    gawk "$2 == \"\\\"Word\\\"\" {print $2}"
    

    这将打印第二个标记为:"Word"的任何行。

    用一些空格将其分解:

    O   B   LQ  WORD  B   LQ  C
    \"  \\  \"  Word  \\  \"  \"
    
    O == Opening quote of the string to search for.
    B == A backslash that will escape the following character
    LQ == A literal double quote (because of the preceding backslash)
    WORD == the literal text 'Word'
    C == Closing quote of the string to search for.
    

    如果测试不在命令行上,看起来会好一些:

    $2 == "\"Word\""
    

    【讨论】:

    • 非常感谢!我整天都在寻找这个!
    • 或者只是把它放在一个文件中,不要反斜杠任何东西!
    猜你喜欢
    • 2015-07-31
    • 1970-01-01
    • 2011-12-09
    • 2020-11-26
    • 1970-01-01
    • 2022-11-10
    • 2013-06-17
    • 2016-11-08
    • 2013-08-09
    相关资源
    最近更新 更多