For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > Splitting fields









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 Splitting fields
lvirden@yahoo.com

2004-03-19, 8:24 pm



I have an app that is reading through a file and reformatting it.
Currently, it expects there to be 12 or more fields, and each one gets
formatted specially.

However, just recently, it was discovered that occasionally some of the
input fields can be so large that they crash into the previous field.

As an example, what normally would :
Jan 12.34 56.0 789.0 ...

shows up as
Jan 12.34 56.0-100.0 ...

Does anyone have a technique by which I could 'loop through' all the fields,
looking for this case (Each time, the field would match [0-9]+-[0-9]+ ),
and a) split the two fields, and b) reset the record so that , for example
in the second example above, I could 'correct' the data so that it
became
Jan 12.34 56.0 -100.0 ...
before the remainder of the processing takes place.

Or am I forced to do that before awk starts reading the data?
--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvirden@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >
John DuBois

2004-03-19, 8:24 pm

In article <c2q4jd$3t0$4@srv38.cas.org>, <lvirden@yahoo.com> wrote:
>
>
>I have an app that is reading through a file and reformatting it.
>Currently, it expects there to be 12 or more fields, and each one gets
>formatted specially.
>
>However, just recently, it was discovered that occasionally some of the
>input fields can be so large that they crash into the previous field.
>
>As an example, what normally would :
>Jan 12.34 56.0 789.0 ...
>
>shows up as
>Jan 12.34 56.0-100.0 ...
>
>Does anyone have a technique by which I could 'loop through' all the fields,
>looking for this case (Each time, the field would match [0-9]+-[0-9]+ ),
>and a) split the two fields, and b) reset the record so that , for example
>in the second example above, I could 'correct' the data so that it
>became
>Jan 12.34 56.0 -100.0 ...
>before the remainder of the processing takes place.


In gawk:

$0 = gensub(/([0-9.]+)(-[0-9.]+)/,"\\1 \\2","g")

John
--
John DuBois spcecdt@armory.com KC6QKZ/AE http://www.armory.com/~spcecdt/
lvirden@yahoo.com

2004-03-19, 8:24 pm


According to John DuBois <spcecdt@deeptht.armory.com>:
:In gawk:
:
: $0 = gensub(/([0-9.]+)(-[0-9.]+)/,"\\1 \\2","g")
:

Awesome - thanks so much! That's really sweet!
I hadn't thought to reassign the resulting string back to $0 to
cause the fields to be recreated!
--
<URL: http://wiki.tcl.tk/ > In God we trust.
Even if explicitly stated to the contrary, nothing in this posting
should be construed as representing my employer's opinions.
<URL: mailto:lvirden@yahoo.com > <URL: http://www.purl.org/NET/lvirden/ >
Sponsored Links







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

Copyright 2008 codecomments.com