awk print ' char

18,805

Solution 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

Solution 2

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

Solution 3

This should work:

cat file.log | awk '{print "nir\047s " $1}'
Share:
18,805

Related videos on Youtube

Nir
Author by

Nir

Updated on September 18, 2022

Comments

  • Nir
    Nir over 1 year

    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}'
    
  • slm
    slm over 10 years
    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.
  • clerksx
    clerksx over 10 years
    @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.
  • maxschlepzig
    maxschlepzig over 10 years
    @slm, you are right. I've removed one useless use in my answer - the first one is quoted from the question.