awk print apostrophe/single quote
Here are a couple of ways:
use octal escape sequences. On ASCII-based systems where
'
is encoded as byte 39 (octal 047), that would be:awk '{print "\047" $0 "\047"}' input 'string1' 'string2' 'string3'
pass the quote as a variable
$ awk -v q="'" '{print q $0 q}' input 'string1' 'string2' 'string3'
In awk code, just put the apostrophe inside double quotes. You can concatenate strings by putting them next to each other (juxtaposition), no matter how these strings are constructed (literal, variable, field reference, parenthesized expression, …).
{ print "'" $0 "'" }
If this awk code is included in a shell script and the awk code is in a single-quoted literal, you have to arrange to pass the single quote character to awk. Use single-quote-backslash-single-quote-single-quote '\''
to include a single quote character in a single-quoted shell literal.
awk '{ print "'\''" $0 "'\''" }'
Alternatively, express the awk code without using a single quote character. You can use a backslash followed by three octal digits to represent any character in a string literal in awk. For a single quote on ASCII based systems, that's \047
.
awk '{ print "\047" $0 "\047" }'
Note that you don't need awk
for that.
You can use the paste
basic utility instead:
< input paste -d "'" /dev/null - /dev/null
It would be more efficient and have fewer limitations than awk
-based or sed
-based solutions.