|
|||
|
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 |
|
|
||||
|
||||
|
|
|
|||
|
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. |
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|