Thread: Re: sub values
View Single Post
  #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