How to wrap this output in quotes?
You could pipe your output to sed
:
tail -3 newtag| awk '{print $1}'| sed "s/^/'/;s/$/'/"
Another way, using hexadecimal ASCII code for '
:
tail -3 newtag | awk '{print "\x27" $1 "\x27" }'
There's the hideous shell quoting hell of these
awk '{print "'"'"'" $1 "'"'"'"}'
awk '{print "'\''" $1 "'\''"}'
or use an odd output field separator
awk -v OFS="'" '{print "", $1, ""}'
but this isn't too bad
awk -v q="'" '{print q $1 q}'
1. One pass with sed
:
tail -3 newtag | sed "s/\(.*\)\s.*/'\1'/"
Explanation: in sed
the replacement string does not have to be quoted
2. Or you could use perl
:
tail -3 newtag | perl -anE "say qq('\$F[0]')"
Explanation:
-
perl
scripting language that excels at text processing -
-a
split each line in fields -
n
do not automatically print each line -
E
execute the following command, and enable features such assay
-
"
start instructions with"
because we later want to use'
-
say
print the following expression and a newline -
qq(
start of literal expression that allows for variable interpolation -
'
a literal'
-
\$F[0]
the first "field" on the line -
'
a literal'
-
)
end of literal expression -
"
end of instructions