Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I have a data file that contains the following data. Q: How do I replace the dashes "-" (using awk gsub) with "Nan" with replacing the minus sign in front of the digits)?
Thanks,
0,11:58:15:028,4095,0,-61.4,1,2,-77,-78,38,-80,-81,38,-,-,-,-,-,-
0,11:58:15:128,4095,0,-61.6,1,2,-76,-78,38,-80,-81,37,-,-,-,-,-,-
0,11:58:15:228,4095,0,-61.6,1,2,-76,-78,38,-80,-81,37,-,-,-,-,-,-
0,11:58:15:328,4095,0,-61.6,1,2,-76,-78,37,-80,-81,37,-,-,-,-,-,-
0,11:58:15:428,4095,0,-61.5,1,2,-76,-77,38,-80,-81,37,-,-,-,-,-,-

I am using Solaris, so I'm using nawk.
One way is to look at each field and only change fields consisting of "-":
#!/bin/ksh nawk ' BEGIN { FS=","; OFS="," } { for(i=1; i<=NF; i++) gsub("^[-]$", "Nan", $i) print $0 } ' datafile.txt

It's been a verrrry long time since I did anything with awk, but wouldn't modifying nails script to:
gsub("^[,-,]$", ",Nan,", $i)
give you what you need?
I don't remember if you have to escape the commas or not.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |