[英]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: 我想明白:
awk
? 在awk
使用单引号是否合法? Why is ''
not a null string like ""
is? 为什么''
不是像""
这样的空字符串? 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专家的上述评论和一些研究,我发布了这个答案:
Further clarification: 进一步澄清:
""
is the null string in awk, not ''
""
是awk中的空字符串,而不是''
"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" }' ...
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的单引号字符串不允许嵌入 '
字符。
'
) in your awk
script , you have three choices: 如果您确实需要在awk
脚本中使用文字单引号( '
) ,则有三种选择:
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
"..."
; 在"..."
使用转义序列 ; 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
'\\''
(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.