1833788 Members
2642 Online
110063 Solutions
New Discussion

Awk Output

 
SOLVED
Go to solution
Ryan B
Frequent Advisor

Awk Output

I have a quick loop that grabs some data and then parses it out and prints with '{print $1}' at the end of the awk statement. Well, if the print for $1 returns a value of Roger, how would I get print statement to show 'Roger' vs just Rogeer with no quotes?

Thanks
4 REPLIES 4
John Poff
Honored Contributor
Solution

Re: Awk Output

Hi,

Here is one way to do it:

awk '{print "\047" $1 "\047"}' somefile

The "\047" specifies the single quote character by specifying the ASCII value of that character.

JP



Dietmar Konermann
Honored Contributor

Re: Awk Output

Hmm... for readability reasons printf may be an option?

{ printf ("'%s'\n", $1); }

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Ryan B
Frequent Advisor

Re: Awk Output

Dietmar~

I tried the "\047" and that works, but I wanted to try yours, but I must be doing something incorrect on the syntax you sent...I get the error '(' not expected...Any thoughts?

Thanks
Dietmar Konermann
Honored Contributor

Re: Awk Output

Hi, Ryan!

The statement I posted is a valid awk statement. However, it looks like you are calling awk from a shell (script), and you use ' to pass the statements to awk. This calling shell interferes, since it interpretes the ' before calling awk.

So I think John's approach is better for your needs.

Best regards...
Dietmar.


"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)