I have the following command, which gives me 99% of what I want:

root@CA2UA5232QPZ:/# tail -3 newtag | awk '{print $1}'

But I need to tweak it so the output looks like this:


You could pipe your output to sed:

tail -3 newtag| awk '{print $1}'| sed "s/^/'/;s/$/'/"
  • 2
    this works but I can't understand the sed pattern you've got going on. can anyone break apart this pattern to explain how it works? – Andy Mar 10 at 4:53
  • I'll take a crack, if I'm wrong I'm sure someone will come along to correct me: sed is a command that replaces text, often times regex like in this case. sed in this case is given 2 separate regexs to replace: s/^/'/ and s/$/'/ The first one says essentially says at the start of a string or line, add a single tick. The second says at the end of a line, add a single tick. Resources: stackoverflow.com/questions/16944357/… + man sed. Beyond my explanation, I recommend reading this: en.wikipedia.org/wiki/Regular_expression#Delimiters – abelito Apr 9 at 2:09

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}'

Another way, using hexadecimal ASCII code for ':

tail -3 newtag | awk '{print "\x27" $1 "\x27" }'

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]')"


  • 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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.