awk: putting quotes around a field value 
Author Message
 awk: putting quotes around a field value

i want to add double-quotes around a field in my awk output.

eg. cat awk.test
aaaa.bbbb.cccc
dddd.eeee.ffff

awk -F. '{print "2nd field is", "$2"}' awk.test

which does NOT give me below what i want:
2nd field is "bbbb"
2nd field is "dddd"

but i usually end up without the quotes, or i get "$2". i have tried
escaping the double quotes with no luck. what am i missing here?

dale kerr
melbourne, australia

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Tue, 11 Feb 2003 09:22:41 GMT  
 awk: putting quotes around a field value

% i want to add double-quotes around a field in my awk output.

[...]
% awk -F. '{print "2nd field is", "$2"}' awk.test
[...]

% but i usually end up without the quotes, or i get "$2". i have tried
% escaping the double quotes with no luck. what am i missing here?

You didn't escape enough. What you want to do is put quotes in a string,
then concatenate that string to the value of $2:

 awk -F. '{print "2nd field is", "\""  $2 "\""}' awk.test

In case that's not clear, suppose you wanted to put <> around $2, you would
do this:
 awk -F. '{print "2nd field is", "<"  $2 ">"}' awk.test

--

Patrick TJ McPhee
East York  Canada



Tue, 11 Feb 2003 10:33:14 GMT  
 awk: putting quotes around a field value
thanks patrick, i did manage to figure it out after awhile. i was
reading that maybe i could specify the ascii equivalent for double
quotes.

can this be done (just for interests sake)?

dale.



Quote:


> % i want to add double-quotes around a field in my awk output.

> [...]
> % awk -F. '{print "2nd field is", "$2"}' awk.test
> [...]

> % but i usually end up without the quotes, or i get "$2". i have tried
> % escaping the double quotes with no luck. what am i missing here?

> You didn't escape enough. What you want to do is put quotes in a
string,
> then concatenate that string to the value of $2:

>  awk -F. '{print "2nd field is", "\""  $2 "\""}' awk.test

> In case that's not clear, suppose you wanted to put <> around $2, you
would
> do this:
>  awk -F. '{print "2nd field is", "<"  $2 ">"}' awk.test

> --

> Patrick TJ McPhee
> East York  Canada


Sent via Deja.com http://www.deja.com/
Before you buy.


Tue, 11 Feb 2003 12:25:37 GMT  
 awk: putting quotes around a field value

Quote:

> ... maybe i could specify the ascii equivalent for double quotes.
> can this be done (just for interests sake)?

printf("%c",ascii_number)
or
sprintf("%c",ascii_number)

--
  All true believers shall break their eggs at the convenient end.



Tue, 11 Feb 2003 03:00:00 GMT  
 awk: putting quotes around a field value

Quote:

> i want to add double-quotes around a field in my awk output.

> eg. cat awk.test
> aaaa.bbbb.cccc
> dddd.eeee.ffff

> awk -F. '{print "2nd field is", "$2"}' awk.test

> which does NOT give me below what i want:
> 2nd field is "bbbb"
> 2nd field is "dddd"

> but i usually end up without the quotes, or i get "$2". i have tried
> escaping the double quotes with no luck. what am i missing here?

> dale kerr
> melbourne, australia

> Sent via Deja.com http://www.deja.com/
> Before you buy.

Dale,

Try this:

awk -F. '{print "2nd field is \""$2"\""}' awk.test

The double quotes are special to awk so you need to escape them to get
them to actually print.

Hope this helps you out.

--
Cheers,
Ted J. Loeffelholz
Senior Design Engineer
Caterpillar Inc



Tue, 11 Feb 2003 03:00:00 GMT  
 awk: putting quotes around a field value


   >> ... maybe i could specify the ascii equivalent for double quotes.
   >> can this be done (just for interests sake)?
   >printf("%c",ascii_number)
   >or
   >sprintf("%c",ascii_number)
Or you could use the octal or hex value:
printf("\042")
printf("\0x22")

Net-Tamer V 1.08X - Test Drive



Tue, 11 Feb 2003 03:00:00 GMT  
 awk: putting quotes around a field value
% thanks patrick, i did manage to figure it out after awhile. i was
% reading that maybe i could specify the ascii equivalent for double
% quotes.

In addition to the reply about using sprintf(), you can put either the octal
or hexadecimal representation of a character into a string by escaping it:
 "\""
 "\042"
 "\x22"
are all equivalent. The octal value must have three digits specified, and the
hex value must have two hexadecimal digits.
--

Patrick TJ McPhee
East York  Canada



Thu, 13 Feb 2003 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Getting awk (or sed or anything else) to put single quotes into file

2. Quoted fields in awk, a general method

3. sqlite select max() on integer field returns quoted value

4. Comparing value in an input field to any value from another file

5. Using field as value for another field

6. Putting Single Quotes in the Output...

7. Putting Single Quotes in the Output...

8. Putting single quotes inside Prolog strings

9. Work around for Memo fields in SQL?

10. printing front panel of VI - no borders appear around the indicator box values

11. Substituting single quotes from Perl via shell using awk and gsub

12. awk not printing single quotes

 

 
Powered by phpBB® Forum Software