awk中单引号和双引号之间的区别 [英] Difference between single and double quotes in awk
问题描述
我有这个awk
声明:
glb_library="my_library"
awk "
/^Direct Dependers of/ { next }
/^---/ { next }
/^$glb_library:/ { ver=\$0; next }
{ gsub(/[[:space:]]/, '', \$0); print ver':'\$0 }
" file
基本上,我已经将awk
代码括在双引号中,以便扩展外壳变量glb_library
.我已确保转义$
字符以防止外壳扩展$0
.遵循了此处的指导.
Basically, I have enclosed the awk
code in double quotes so that the shell variable glb_library
is expanded. I have made sure to escape the $
character to prevent the shell from expanding $0
. Followed the guidance from here.
awk
给我这个错误:
awk: syntax error at source line 5
context is
{ gsub(/[[:space:]]/, >>> ' <<<
我想了解:
- 在
awk
中使用单引号是否合法?为什么''
不是像""
这样的空字符串? -
awk
处理单引号和双引号是否不同?
- Is it legal to use single quotes inside
awk
? Why is''
not a null string like""
is? - Does
awk
treat single and double quotes differently?
在用反斜杠转义单引号并使用\"\"
表示空字符串而不是''
之后,我的代码工作了.
My code worked after I escaped the single quotes with backslashes and used \"\"
to represent the null string instead of ''
.
推荐答案
基于awk专家的上述评论和一些研究,我正在发布此答案:
Based on the comments above by awk experts and some research, I am posting this answer:
- awk字符串用双引号而不是单引号引起来;更准确地说: 单引号不是awk中的字符串定界符,与shell不同
- awk对单引号没有特殊含义,并且如果用于字符串文字中,则必须将其用双引号引起来
- 最好使用单引号在命令行上包装awk语句,这与OP的代码使用双引号不同(Ed明确指出了这一点)
进一步澄清:
-
""
是awk中的空字符串,不是''
- 要在awk字符串文字中使用单引号,请使用双引号将其引起来,如
"Ed's answers are great!"
-
在awk中处理单引号时遵循的其他技术是:
""
is the null string in awk, not''
- to use single quotes in an awk string literal, enclose them in double quotes, as in
"Ed's answers are great!"
other techniques followed while handling single quotes in awk are:
a)使用变量,如awk -v q="'" '{ print q }' ...
b)使用八进制或十六进制表示法,如awk '{ print "\047"$0"\047" }' ...
b) use octal or hex notation, as in awk '{ print "\047"$0"\047" }' ...
相关文档此处.
这篇关于awk中单引号和双引号之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!