Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

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
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
add comment

This should work:

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

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
add comment

Your Answer

 
discard

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.