Difference between single and double quotes in awk

I have an instruction 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 enclosed the code awkin double quotes, so the shell variable glb_libraryexpands. I am sure to avoid the character $to prevent shell expansion $0. Following the directions here .

awk gives me this error:

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

I want to understand:

  • Is it possible to use single quotes inside awk? Why ''not an empty string, for example ""?
  • Does awksingle and double quotes separate ?

My code worked after I escaped single quotes with \"\"backslashes and used to represent a null string instead ''.

+6
3

awk , :

  • awk , ; : awk,
  • awk , ,
  • awk , OP, ( )

:

  • "" - awk, ''
  • awk, , "Ed answers are great!"
  • , awk:

    a) , awk -v q="'" '{ print q }' ...

    b) , awk '{ print "\047"$0"\047" }' ...


.

+3

script - . , :

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
+6

:

  • :
    , awk ('...'), , , awk.

  • awk, ("...").

    • " - awk.
    • "..." ( ), , \n \t.
  • (') awk, , - '...' , - , ' , '.

    • (') awk, :
      • , , awk :
        awk -v q=\' 'BEGIN { print "I" q "m good." }' # -> I'm good
      • Use the escape sequence inside "..."; For maximum portability and disambiguation, use the octal escape sequence ( \047), not the hexadecimal ( \x27):
        awk 'BEGIN { print "I\047m good." }' # -> I'm good
      • Use '\''(sic) to "exit" from embedded characters '. (3 separate shell literals in single quotes are technically combined) Thanks, snr :
        awk 'BEGIN { print "I'\''m good" }' # -> I'm good
+3
source

Source: https://habr.com/ru/post/1678825/


All Articles