Celeb Glow
news | March 11, 2026

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:

  • 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 as say
  • " 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy