How to wrap this output in quotes?
I have the following command, which gives me 99% of what I want:
root@CA2UA5232QPZ:/# tail -3 newtag | awk '{print $1}'
v1.0.20170512.1
v1.0.20170712.1
v1.0.20170712.2
root@CA2UA5232QPZ:/#But I need to tweak it so the output looks like this:
'v1.0.20170512.1'
'v1.0.20170712.1'
'v1.0.20170712.2' 0 4 Answers
You could pipe your output to sed:
tail -3 newtag| awk '{print $1}'| sed "s/^/'/;s/$/'/" 2 Another way, using hexadecimal ASCII code for ':
tail -3 newtag | awk '{print "\x27" $1 "\x27" }' 3 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:
perlscripting language that excels at text processing-asplit each line in fieldsndo not automatically print each lineEexecute the following command, and enable features such assay"start instructions with"because we later want to use'sayprint the following expression and a newlineqq(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