I want to print:

cat file.log | awk '{print nir's $1}'

output should be:

nir's aaa
nir's bbb
nir's abc
nir's dbc

The problem is with the ' in nir's.

I also tried:

cat file.log | awk '{print nir\'s $1}'
cat file.log | awk '{print nir''s $1}'
cat file.log | awk '{print nir'''s $1}'

You have to quote the single quote to protect it from the shell. That means instead of

$ cat file.log | awk '{print nir's $1}'

you have to write something like

$ awk '{print "nir'"'"'s",$1}' file.log
  • 1
    Useless use of cat. You don't need to cat the file file.log to awk. The command awk can deal with the filename directly. – slm Dec 22 '13 at 15:20
  • 2
    @slm Not to mention that it's not merely an issue of semantics, some optimisations may not be able to be applied when reading from a stream vs. reading from a file. – Chris Down Dec 22 '13 at 15:35
  • @slm, you are right. I've removed one useless use in my answer - the first one is quoted from the question. – maxschlepzig Dec 22 '13 at 15:44

There are two problems here:

  1. You can't pass ' within ', which you are also using to quote the awk syntax. You need to leave the quoting first;
  2. You need to quote strings in awk.

It seems like this is what you want:

awk '{print "nir'\''s " $1}'

This should work:

cat file.log | awk '{print "nir\047s " $1}'

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.