Computing.Net > Forums > Unix > substituting dash instead of minus sign awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

substituting dash instead of minus sign awk

Reply to Message Icon

Name: Choc
Date: October 7, 2009 at 09:13:10 Pacific
OS: Unix
Subcategory: Software Problems
Comment:

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,-,-,-,-,-,-



Sponsored Link
Ads by Google

Response Number 1
Name: Choc
Date: October 7, 2009 at 09:14:37 Pacific
Reply:

opps, I mean ... without replacing the minus sign in front of the digits)?


0

Response Number 2
Name: nails
Date: October 7, 2009 at 13:21:32 Pacific
Reply:

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


0

Response Number 3
Name: Choc
Date: October 8, 2009 at 00:13:34 Pacific
Reply:

Thanks nails.

I was hoping to avoid checking each field...


0

Response Number 4
Name: Mike (by mmcconaghy)
Date: October 8, 2009 at 05:13:08 Pacific
Reply:

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.


MIKE

http://www.skeptic.com/


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: substituting dash instead of minus sign awk

AWK Question www.computing.net/answers/unix/awk-question/4512.html

UNIX Sed remove add www.computing.net/answers/unix/unix-sed-remove-add/7205.html

script-need help www.computing.net/answers/unix/scriptneed-help/6997.html