Question about awk format

Hi, Guys,
There is one question about AWK format. Here is the code:

gawk -F: '/^Dan/ {print "Dan's phone number is ",$2}' lab3.data

An syntax error will come out because the quote mark between Dan and s and first quote mark are recognized as a quote pair.
I want to get the input like this:

I know if I put the awk program instruction into a file, it will work well. However, is there anything else to solve it in the command line?
Thanks.

It's not an awk problem, it's a shell problem. Single quotes inside single quotes are taken to end a single quote block and there's no way to escape them inside single quotes (since you can't escape anything in single quotes, at all, ever.)

You could put the string in a variable:

gawk -F: '/^Dan/ {print STR, $2}' STR="Dan's phone number is" lab3.data

Or just put everything in double quotes and escape everything that needs escaping, but that's a bit more elaborate than needed here.

Or you could use the octal code for single quote:

gawk -F: '/^Dan/ {print "Dan\047s phone number is ",$2}' lab3.data

Cool! Thanks!:smiley:

---------- Post updated at 03:03 PM ---------- Previous update was at 03:01 PM ----------

Smart!:smiley:

The following is more general (not assuming ASCII codes):

gawk -F: '/^Dan/ {print "Dan'\''s phone number is ",$2}' lab3.data

The shell sees 'string1' \' 'string2' , so the shell does evaluation of \' outside a 'string' .
The awk sees the evaluated '

1 Like

And the inevitable:

gawk -F: "/^Dan/ {print \"Dan's phone number is\",\$2}"  file