Avoiding a single quote inside awk

I want to do the following

awk 'BEGIN {FS=" ";} {printf "'%s' ", $1}' 

But escaping a single quote this way doesn't work

 awk 'BEGIN {FS=" ";} {printf "\'%s\' ", $1}' 

How to do it? Thanks for the help.

+63
awk
Mar 27 '12 at 23:06
source share
5 answers

Perhaps this is what you are looking for:

 awk 'BEGIN {FS=" ";} {printf "'\''%s'\'' ", $1}' 

That is, with '\'' you close the opening ' , and then print the literal ' , escaping it, and finally open it again. '

+95
Mar 28 2018-12-12T00:
source share

A single quote is represented using \x27

How in

 awk 'BEGIN {FS=" ";} {printf "\x27%s\x27 ", $1}' 

Source

+50
Oct 11 '12 at 17:29
source share

Another option is to pass a single quote as an awk variable:

 awk -vq=\' 'BEGIN {FS=" ";} {printf "%s%s%s ", q, $1, q}' 

Example:

  # Prints 'test me', _including_ the single quotes. awk -vq=\' '{print q $0 q }' <<<'test me' 
+18
Apr 16 '14 at 19:58
source share
 awk 'BEGIN {FS=" "} {printf "\047%s\047 ", $1}' 
+6
Dec 18 '13 at 17:24
source share
 $ cat > test.in foo bar $ awk 'BEGIN {FS=" ";} {printf "'"'"'%s'"'"' ", $1}' test.in 'foo' 'bar' 

These are: '"'"' in double quotes.

-one
Aug 03 '16 at 5:53 on
source share



All Articles