Difference between single and double quotes in awk

2020-01-31 04:16发布

I have this awk statement:

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. I have made sure to escape the $ character to prevent the shell from expanding $0. Followed the guidance from here.

awk gives me this error:

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

I want to understand:

  • 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 ''.

标签: bash awk quotes
3条回答
在下西门庆
2楼-- · 2020-01-31 04:36

Based on the comments above by awk experts and some research, I am posting this answer:

  • awk strings are enclosed in double quotes, not single quotes; more precisely: single quotes are not string delimiters in awk, unlike shell
  • awk attaches no special meaning to single quotes and they need to be enclosed in double quotes if used in string literals
  • 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)

Further clarification:

  • "" 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) use a variable, as in awk -v q="'" '{ print q }' ...

    b) use octal or hex notation, as in awk '{ print "\047"$0"\047" }' ...


Relevant documentation here.

查看更多
三岁会撩人
3楼-- · 2020-01-31 04:38

A pragmatic summary:

  • As Ed Morton's helpful answer sensibly recommends:
    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.

  • To define strings inside an awk script, always use double quotes ("...").

    • " is the only string delimiter awk recognizes.
    • "..." strings are non-interpolating (you cannot embed variable references), but they do recognize control-character sequences such as \n and \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.

    • If you do need to use a literal single quote (') in your awk script, you have three choices:
      • Pass a variable that defines it, and use awk's string concatenation, based on directly adjoining string literals and variable references:
        awk -v q=\' '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):
        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:
        awk 'BEGIN { print "I'\''m good" }' # -> I'm good
查看更多
何必那么认真
4楼-- · 2020-01-31 04:40

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
查看更多
登录 后发表回答