For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2005 > Re: is gensub the right one here?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: is gensub the right one here?
Sebastian Luque

2005-05-17, 3:56 am

Sebastian Luque <sluque@mun.ca> wrote:

[...]

> FNR > 1 {
> a = gensub(/(=-?[[:digit:]]*),?([[:digit:]]*)/, "\\1.\\2", 1);
> b = gensub(/(=-?[[:digit:]]*),?([[:digit:]]*)/, "\\1.\\2", 2);
> c = gensub(/(=-?[[:digit:]]*),?([[:digit:]]*)/, "\\1.\\2", 3);
> print a b c;
> }


A better stab:

FNR > 1 {
$0 = gensub(/=(-? [[:digit:]]+),([[:digit:]]+)(,[[:alpha:]
]|$)/, "=\\1.\\2\\3", "g")
print
}

but this is not correctly modifying the decimal separator in the last
field. I'm not understanding how my "or" is working in that last
parenthesis in the regexp.

Cheers,
--
Sebastian P. Luque
Ed Morton

2005-05-17, 3:55 pm



Sebastian Luque wrote:
> Sebastian Luque <sluque@mun.ca> wrote:
>
> [...]
>
>
>
>
> I think I got it:
>
> FNR > 1 {
> $0 = gensub(/=(-? [[:digit:]]+),([[:digit:]]+),([[:alpha:]
]*|$)/, "=\\1.\\2,\\3", "g")
> print
> }



It doesn't need to be quite that complicated for the sample you showed:

> "Date","Time","Moisture","Grade","Temperature",
> Dec-16-2001,18:10:00,Moist=,Grad=,Temp=,
> Dec-16-2001,18:10:05,Moist=50,2,Grad=,Temp=5.3,
> Dec-16-2001,18:10:10,Moist=70,05,Grad=30.5,Temp=3,45,
> Dec-16-2001,18:10:15,Moist=75.3,Grad=20,Temp=12,5,


Just looking for an equals followed by digits, comma, digits, comma and
replacing that first comma would do it:

gensub(/(=[[:digit:]]+),([[:digit:]]+,)/,"\\1.\\2","g")

Regards,

Ed.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com