简体   繁体   English

awk中单引号和双引号之间的区别

[英]Difference between single and double quotes in awk

I have this awk statement: 我有这个awk声明:

glb_library="my_library"
awk "
        /^Direct Dependers of/ { next }
        /^---/                 { next }
        /^$glb_library:/       { ver=\$0; next }
                               { gsub(/[[:space:]]/, '', \$0); print ver':'\$0 }
      " file

Basically, I have enclosed the awk code in double quotes so that the shell variable glb_library is expanded. 基本上,我将awk代码用双引号括起来,以便扩展shell变量glb_library I have made sure to escape the $ character to prevent the shell from expanding $0 . 我确保逃避$字符以防止shell扩展$0 Followed the guidance from here . 遵循这里的指导。

awk gives me this error: awk给了我这个错误:

awk: syntax error at source line 5
 context is
                                   { gsub(/[[:space:]]/, >>>  ' <<<

I want to understand: 我想明白:

  • Is it legal to use single quotes inside awk ? awk使用单引号是否合法? Why is '' not a null string like "" is? 为什么''不是像""这样的空字符串?
  • Does awk treat single and double quotes differently? awk对待单引号和双引号有何不同?

My code worked after I escaped the single quotes with backslashes and used \\"\\" to represent the null string instead of '' . 在我使用反斜杠转义单引号并使用\\"\\"表示空字符串而不是''之后,我的代码工作正常。

Never enclose any script in double quotes or you're sentencing yourself to backslash-hell. 永远不要用双引号括起任何脚本,否则你就是在判断自己的反斜杠地狱。 This is the syntax for what you're trying to do: 这是您尝试执行的操作的语法:

glb_library="my_library"
awk -v glb_library="$glb_library" '
        /^Direct Dependers of/ { next }
        /^---/                 { next }
        $0 ~ "^"glb_library":" { ver=$0; next }
                               { gsub(/[[:space:]]/, ""); print ver":"$0 }
      ' file

Based on the comments above by awk experts and some research, I am posting this answer: 基于awk专家的上述评论和一些研究,我发布了这个答案:

  • awk strings are enclosed in double quotes, not single quotes; awk字符串用双引号括起来,而不是单引号; more precisely: single quotes are not string delimiters in awk, unlike shell 更确切地说: 单引号在awk中不是字符串分隔符,与shell不同
  • awk attaches no special meaning to single quotes and they need to be enclosed in double quotes if used in string literals awk对单引号没有特殊含义,如果在字符串文字中使用,则需要用双引号括起来
  • it is best to use single quotes to wrap awk statements on command line, unlike OP's code that's using double quotes (Ed pointed this out clearly) 最好使用单引号在命令行上包装awk语句,这与OP的使用双引号的代码不同(Ed明确指出了这一点)

Further clarification: 进一步澄清:

  • "" is the null string in awk, not '' ""是awk中的空字符串,而不是''
  • to use single quotes in an awk string literal, enclose them in double quotes, as in "Ed's answers are great!" 在awk字符串文字中使用单引号,用双引号括起来,如"Ed's answers are great!"
  • other techniques followed while handling single quotes in awk are: 处理awk中单引号时遵循的其他技巧是:

    a) use a variable, as in awk -vq="'" '{ print q }' ... a)使用变量,如awk -vq="'" '{ print q }' ...

    b) use octal or hex notation, as in awk '{ print "\\047"$0"\\047" }' ... b)使用八进制或十六进制表示法,如awk '{ print "\\047"$0"\\047" }' ...


Relevant documentation here . 相关文档在这里

A pragmatic summary: 务实的总结:

  • As Ed Morton's helpful answer sensibly recommends: 正如Ed Morton的有用答案明智地建议:
    Always use single quotes to enclose your awk script as a whole ( '...' ) , which ensures that there's no confusion over what the shell interprets up front , and what awk ends up seeing. 始终使用引号括住你awk 脚本作为一个整体'...' ),从而确保有在什么解释了前面没有混乱,什么awk最终看到的。

  • To define strings inside an awk script, always use double quotes ( "..." ) . awk脚本中定义字符串,请始终使用引号( "..."

    • " is the only string delimiter awk recognizes. "awk识别的唯一字符串分隔符。
    • "..." strings are non-interpolating (you cannot embed variable references), but they do recognize control-character sequences such as \\n and \\t . "..."字符串是非插值的 (您不能嵌入变量引用),但它们确实识别控制字符序列,例如\\n\\t
  • A single quote ( ' ) has no syntactic meaning inside an awk script , but, - if you're using '...' for your overall script, as recommended - you cannot use a literal ' inside of it anyway , because the shell's single-quoted strings do not permit embedded ' chars. 单引号( ' )在awk脚本中没有语法含义 ,但是, - 如果你正在使用'...'作为你的整个脚本,按照建议 - 无论如何都不能使用 ' 内部文字 ,因为shell的单引号字符串不允许嵌入 '字符。

    • If you do need to use a literal single quote ( ' ) in your awk script , you have three choices: 如果您确实需要在awk脚本中使用文字单引号( ' ,则有三种选择:
      • Pass a variable that defines it, and use awk 's string concatenation, based on directly adjoining string literals and variable references: 传递一个定义它的变量 ,并使用awk的字符串连接,基于直接相邻的字符串文字和变量引用:
        awk -vq=\\' 'BEGIN { print "I" q "m good." }' # -> I'm good
      • Use an escape sequence inside "..." ; "..."使用转义序列 ; for maximum portability and disambiguation, use an octal escape sequence ( \\047 ), not a hex one ( \\x27 ): 为了获得最大的可移植性和消歧,请使用八进制转义序列( \\047 ),而不是十六 进制转换序列( \\x27 ):
        awk 'BEGIN { print "I\\047m good." }' # -> I'm good
      • Use '\\'' (sic) to "escape" embedded ' chars. 使用'\\'' (原文如此)来“逃避”嵌入的'字符' (technically, 3 distinct single-quoted shell string literals are being concatenated) Thanks, snr : (从技术上讲,3个不同的单引号shell字符串文字正在连接) 谢谢, snr
        awk 'BEGIN { print "I'\\''m good" }' # -> I'm good

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM