awk printing ' character (81 Views)
Reply
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 1 of 18 (81 Views)
Accepted Solution

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.
Please use plain text.
Advisor
Walter Goncalves
Posts: 19
Registered: ‎06-11-2003
Message 2 of 18 (81 Views)

Re: awk printing ' character

Hi try

awk '{print $x}'

where x is the field number of your command
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 3 of 18 (81 Views)

Re: awk printing ' character

awk '{ print $1 }'

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


Pete


Pete
Please use plain text.
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 4 of 18 (81 Views)

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
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 5 of 18 (81 Views)

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.
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 6 of 18 (81 Views)

Re: awk printing ' character

That flip line seems to do the trick.

Hmmm got out of sequence a bit there.
Please use plain text.
Outstanding Contributor
Pete Randall
Posts: 16,205
Registered: ‎11-03-1996
Message 7 of 18 (81 Views)

Re: awk printing ' character

Gavin,

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


Pete


Pete
Please use plain text.
Honored Contributor
john korterman
Posts: 1,117
Registered: ‎11-15-2000
Message 8 of 18 (81 Views)

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
Please use plain text.
Honored Contributor
curt larson_1
Posts: 764
Registered: ‎08-23-2002
Message 9 of 18 (81 Views)

Re: awk printing ' character

have you tried:
print "\'" $1 "\'";
or
printf("'%s'\n",$1);
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 10 of 18 (81 Views)

Re: awk printing ' character

Hi Gavin:

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

Regards!

...JRF...
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 11 of 18 (81 Views)

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.
Please use plain text.
Honored Contributor
Jean-Luc Oudart
Posts: 1,461
Registered: ‎03-10-1998
Message 12 of 18 (81 Views)

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
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 13 of 18 (81 Views)

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.
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 14 of 18 (81 Views)

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...
Please use plain text.
Occasional Visitor
karthik3152
Posts: 1
Registered: ‎06-05-2011
Message 15 of 18 (81 Views)

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.







Please use plain text.
Acclaimed Contributor
Dennis Handly
Posts: 24,784
Registered: ‎03-06-2006
Message 16 of 18 (81 Views)

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.
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 17 of 18 (81 Views)

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.
Please use plain text.
Trusted Contributor
Gavin Clarke
Posts: 456
Registered: ‎06-05-1997
Message 18 of 18 (81 Views)

Re: awk printing ' character

There are some right answers in here to what I found to be a tricky awk problem at the time.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation