Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.* 1 > Newsgroup comp.lang.awk

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2012, 05:05 AM
Eric Pement
Guest
 
Posts: n/a
Default Re: sub values

On Wednesday, March 28, 2012 4:58:50 AM UTC-5, Steve wrote:
> data file looks like this...
>
> 0 12 43
> 12 120 30
> 20 45 0
> ..............
>
> I want substitute all 0 values with NA. I have it working the long way
> round like so,
>
> awk '{sub(" 0 ", " NA "); print $0}'
> awk '{sub(" 0", " NA"); print $0}'
> awk '{sub("0 ", "NA "); print $0}'
>
> Trying to learn a bit so my question is this. Is there a simple way of
> telling awk to treat 0 as a value, rather than a character when using
> sub command.


Try this:
awk '{gsub(/\<0\>/, "NA")}; 1'

Eric
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 04-02-2012, 09:03 PM
Ed Morton
Guest
 
Posts: n/a
Default Re: sub values

On 3/30/2012 12:05 AM, Eric Pement wrote:
> On Wednesday, March 28, 2012 4:58:50 AM UTC-5, Steve wrote:
>> data file looks like this...
>>
>> 0 12 43
>> 12 120 30
>> 20 45 0
>> ..............
>>
>> I want substitute all 0 values with NA. I have it working the long way
>> round like so,
>>
>> awk '{sub(" 0 ", " NA "); print $0}'
>> awk '{sub(" 0", " NA"); print $0}'
>> awk '{sub("0 ", "NA "); print $0}'
>>
>> Trying to learn a bit so my question is this. Is there a simple way of
>> telling awk to treat 0 as a value, rather than a character when using
>> sub command.

>
> Try this:
> awk '{gsub(/\<0\>/, "NA")}; 1'
>
> Eric


Won't work if the number is 00 or 0.0 or ....

I may be reading too much into the OPs requirement to handle "all 0 values" though.

Ed.
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 08:14 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.