I want do the following
awk 'BEGIN {FS=" ";} {printf "'%s' ", $1}'
But escaping single quote this way does not work
awk 'BEGIN {FS=" ";} {printf "\'%s\' ", $1}'
How to do this? Thanks for help.
This maybe what you're looking for:
awk 'BEGIN {FS=" ";} {printf "'\''%s'\'' ", $1}'
That is, with '\''
you close the opening '
, then print a literal '
by escaping it and finally open the '
again.
'
character closes the opening '
shell string literal. The shell literal does not support a backslash escape for this. The sequence '\''
does the trick: it closes the single-quote literal, specifies the quote character (using an escape that is supported outside of single-quote literals) and then re-opens a new single-quote literal. You can think of it as a four-character escape sequence to get a single quote. :)
awk
are purely a matter of the command interpreter you are using to compose command lines. The '{printf $2}'
gets turned into some argument for an execve
system call or similar, where it just looks like a null terminated C string without any single quotes. Awk never sees the quotes, and neither does sed. You can in fact use double-quotes, but double quotes do not prevent the shell's expansion of $2
, so you have to escape the dollar sign with a backslash to make it literal: "{printf \$2}"
.
sed -e "s/$FOO/$BAR/"
will not work if the intent is to replace the literal text $FOO
with $BAR
. The easiest way would be sed -e 's/$FOO/$BAR/
.
awk "{ print \"abc\", \$1 }"
. Any time a double quote appears in the awk program, it has to be escaped so that it doesn't close the shell quote. And compare this: awk '{print "\\"}'
(print a backslash) versus what it takes with double quotes: awk "BEGIN {print \"\\\\\" }"
, Phew! Both quotes have to be escaped, and both backslashes. The shell converts \\
to \
so we need \\\\
to encode \\
.
A single quote is represented using \x27
Like in
awk 'BEGIN {FS=" ";} {printf "\x27%s\x27 ", $1}'
\x27
is an extension; POSIX Awk only recognizes \047
. (\47
is ok too if not followed by an octal digit.)
\047
), not hex (\x27
), escape codes - see awk.freeshell.org/PrintASingleQuote.
Commented
Aug 1, 2016 at 13:02
Another option is to pass the single quote as an awk variable:
awk -v q=\' 'BEGIN {FS=" ";} {printf "%s%s%s ", q, $1, q}'
Simple example with string concatenation:
# Prints 'test me', *including* the single quotes.
awk -v q=\' '{ print q $0 q }' <<<'test me'
awk 'BEGIN {FS=" "} {printf "\047%s\047 ", $1}'
\047
is the octal escape sequence for the single-quote character, I find this alternative to be the most readable.
Commented
Jan 11, 2019 at 17:54
For small scripts, an optional way to make it readable is to use a variable like this:
awk -v fmt="'%s'\n" '{printf fmt, $1}'
I found it convenient in a case where I had to produce many times the single-quote character in the output and the \047
were making it totally unreadable.
When you are using awk in the command line, you don't have to use the BEGIN block to define the field separator (FS) you can only use -F" " like:
awk -F" " {printf "\047%s\047 ", $1}'
saves you some typing. :)
this is how to print a single quote in awk: "\047"
example:
for i in aA bB cC ; do echo $i | awk '{print "\047"$1"\047"}' ; done
'aA'
'bB'
'cC'
UPDATE 1 : more hands free approach to the $1
single quoting problem :
1 echo " 123 xyz 456 abc "
'123'
{m,g}awk 'BEGIN { __=_
OFS = sprintf("%c",
++_ +_++*_+_++^++_+_*++_)
_+=_^= FS = "^|[[:space:]]+"
} NF +=_ ==( NF = _)'
===========================
mawk
has, by far, the most concise syntax to wrap lines in single quotes without a printf
statement :
gawk 'NF+= substr(!_, $2 =$_ ($_=_))' FS='^$' OFS='\47'
nawk '$2 =$-_ substr(_, $-_=_, NF = 3)' FS='^$' OFS='\47'
mawk '$++NF=$_ ($_=_)' FS=^$ OFS=\\47
INPUT
<( [[[a]]]bc:
뀿 123=)>
OUTPUT
' [[[a]]]bc:
뀿 123='
I intentionally wrapped it in <(…)>
to point out there's leading edge space. The gap in between the two lines is a \f : form feed \014
.
That said, this only wraps lines without escaping single quotes within the line itself.
To use sub()
instead, it'll look like :
{m,n,g}awk -F'^$' 'sub(".*","\47&\47")'
Another way: awk 'BEGIN {FS=" ";} {printf "%c%s%c ", 39, $1, 39}'
Use %c
and give the ASCII code number for single quotes, which is 39.
By the way, if you need to print double quotes, the ASCII code is 34!
'abc'd'ef'
isabcdef
: literal plusd
plus literal. Thed
is outside of the quotes, and you can replace thatd
it with\'
to make'abc'\''ef'
which evaluates toabc'ef
.