If you have tried printing a single quote ('
) with awk
,
you might have found that using single quotes with awk
can be a bit tricky.
This is due to the fact that '
is a shell string literal.
A regular escape using backslash does not work.
It can be done with '\''
.
Example:
awk '{ print "'\''" $2 "'\''" }'
This will output the content of $2
surrounded by single quotes.
'\''
works like this:
- The first
'
closes the opening single quote. - Then
\'
prints a literal'
by escaping it. - The final
'
opens the single quote again.