Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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}'
share|improve this question
up vote 3 down vote accepted

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
share|improve this answer
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
@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}'
share|improve this answer

This should work:

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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