System Administration
Showing results for 
Search instead for 
Do you mean 

awk printing ' character

SOLVED
Go to Solution
Trusted Contributor Trusted Contributor

awk printing ' character

How do I print ' as part of an awk command, I can't seem to find the right way to do it.

ls | awk '{print "'" $1 "'"}' just doesn't work.

Thanks very much in anticipation of some stunning insights.
17 REPLIES

Re: awk printing ' character

Hi try

awk '{print $x}'

where x is the field number of your command
Outstanding Contributor Outstanding Contributor

Re: awk printing ' character

awk '{ print $1 }'

should work. Cut and paste it to make sure you get the spacing right.


Pete


Pete
Honored Contributor Honored Contributor

Re: awk printing ' character

Hi,
the only way I can think of:
# echo hello | awk -v flip="\'" '{print flip $1 }'

regards,
John K.
it would be nice if you always got a second chance
Trusted Contributor Trusted Contributor

Re: awk printing ' character

Sorry that's not going to work. I need to print the character ' round either side of the filename ($1).

Thanks for a quick reply though.
Trusted Contributor Trusted Contributor

Re: awk printing ' character

That flip line seems to do the trick.

Hmmm got out of sequence a bit there.
Outstanding Contributor Outstanding Contributor

Re: awk printing ' character

Gavin,

Sorry - I missed that you're trying to print the apostrophe. Working . . .


Pete


Pete
Honored Contributor Honored Contributor

Re: awk printing ' character

sorry,
should of course have been:
# ls | awk -v flip="\'" '{print flip $1 flip }'

regards,
John K.
it would be nice if you always got a second chance
Honored Contributor Honored Contributor

Re: awk printing ' character

have you tried:
print "\'" $1 "\'";
or
printf("'%s'\n",$1);
Acclaimed Contributor Acclaimed Contributor

Re: awk printing ' character

Hi Gavin:

# ls -l /tmp/me|awk '{print "'\''" $NF "'\''"}'

Regards!

...JRF...
Trusted Contributor Trusted Contributor

Re: awk printing ' character

I did try using printf, I still had problems.

If flip doesn't work then I'll give it another go.
Honored Contributor Honored Contributor

Re: awk printing ' character

I had same issue a while ago
#!/bin/sh

QUOT="'"

echo " " | awk -v quot=$QUOT '
{ print quot,quot,"abcd",quot,quot;}'

just an example.

Rgds,
Jean-luc
fiat lux
Trusted Contributor Trusted Contributor

Re: awk printing ' character

Whoa, thanks for all the responses. After some fiddling I got what I wanted, which was a little more complicated than the example I gave (but not alot).

I adopted the "'\''" approach after I'd cut and paste it into notepad to see what was actually going on, seemed to fit well with what I was doing.
Acclaimed Contributor Acclaimed Contributor

Re: awk printing ' character

Hi (again) Gavin:

For clarity later, the post of mine you used:

# ls -l /tmp/me|awk '{print "'\''" $NF "'\''"}'

is garbled badly when it is displayed here. For reference, the character sequence is:

...print $NF singlequote> )

Regards!

...JRF...
Occasional Visitor

Re: awk printing ' character

do it this way

ls | awk '{print $1}'


For clearer explanation go through the AWK PROGRAMMING MODEL
which explains how the awk works
http://linux-forum-karthik.blogspot.com/2011/05/awk-programming-model.html

and go through the awk variables usuage

http://linux-forum-karthik.blogspot.com/2011/05/awk-variables.html

Above links will be useful in learning the same.







Acclaimed Contributor Acclaimed Contributor

Re: awk printing ' character

You don't. A single quote is used to quote the whole awk script. If you want to use a single quote, you need to use an awk program file then you don't need to quote:
ls | evil_quoting_script.awk
In the script:
#!/usr/bin/awk -f
{print "'" $1 "'"}

Or you can do it the direct (right) way with escapes:
ls | awk '{print "\x27" $1 "\x27"}'

>karthik3152: do it this way

The OP has said several times that's not what he wants.
Trusted Contributor Trusted Contributor

Re: awk printing ' character

I should also point out that I asked this question approximately eight years ago!

I suppose I'd better close the thread.
Highlighted
Trusted Contributor Trusted Contributor

Re: awk printing ' character

There are some right answers in here to what I found to be a tricky awk problem at the time.