For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2007 > Scanning fixed field size file









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 Scanning fixed field size file
eldiabloinfernal

2007-01-30, 6:57 pm

Hi Gurus
I have a problem that i would like to resolve using awk. I have a file
that contains approx 20K records. The records are fixed length and
start at fixed positions. each line has 20 fields (one record). I
would like to extract 4 fields and split one of these fields into two
separate fields. i have experimented with the substr functions and i
can do what i want. can anyone help me put toghether a script to do it
all?

the fields are as follows

field position length type
3 10 8 ascii
7 25 8 date
8 34 6 time
12 64 10 ascii

any help appreciated

regards
dean

Janis Papanagnou

2007-01-30, 6:57 pm

eldiabloinfernal wrote:
> Hi Gurus
> I have a problem that i would like to resolve using awk. I have a file
> that contains approx 20K records. The records are fixed length and
> start at fixed positions. each line has 20 fields (one record). I
> would like to extract 4 fields and split one of these fields into two
> separate fields. i have experimented with the substr functions and i
> can do what i want. can anyone help me put toghether a script to do it
> all?


Use GNU awk and make use of the FIELDWIDTHS feature. Something like the
following code; though I haven't precisely counted the field widths you
mention below, so adjust the numbers as necessary...

BEGIN { FIELDWIDTHS = "5 4 8 2 2 3 8 6 10 10 10 10" }
{ print $3, $7, $8, $12 }

To split one of the four fields in two parts use function substr() as
you've already experimented with, I suppose you know how to use it.

Janis

>
> the fields are as follows
>
> field position length type
> 3 10 8 ascii
> 7 25 8 date
> 8 34 6 time
> 12 64 10 ascii
>
> any help appreciated
>
> regards
> dean
>

eldiabloinfernal

2007-02-01, 7:57 am

On Jan 30, 6:38 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> eldiabloinfernal wrote:
>
> Use GNU awk and make use of the FIELDWIDTHS feature. Something like the
> following code; though I haven't precisely counted the field widths you
> mention below, so adjust the numbers as necessary...
>
> BEGIN { FIELDWIDTHS = "5 4 8 2 2 3 8 6 10 10 10 10" }
> { print $3, $7, $8, $12 }
>
> To split one of the four fields in two parts use function substr() as
> you've already experimented with, I suppose you know how to use it.
>
> Janis
>
>
>
>
>
>
>
>
>
> - Show quoted text -


Hello

Thanks for your reply however i do not have gawk, only plain and
simple unix (hpux) awk .... can i do this with awk? any ideas??

regards
dean

Kenny McCormack

2007-02-01, 7:57 am

In article <1170332552.173796.49150@v33g2000cwv.googlegroups.com>,
eldiabloinfernal <diabolik@uku.co.uk> wrote:
....
>Thanks for your reply however i do not have gawk, only plain and
>simple unix (hpux) awk .... can i do this with awk? any ideas??


Do you have a compiler and an internet connection?

If so, then you have gawk. Strongly recommended. Using old, icky AWKs
is just asking for ongoing pain.

Ed Morton

2007-02-01, 7:57 am

eldiabloinfernal wrote:
> On Jan 30, 6:38 pm, Janis Papanagnou <Janis_Papanag...@hotmail.com>
> wrote:
>
>
>
> Hello
>
> Thanks for your reply however i do not have gawk, only plain and
> simple unix (hpux) awk .... can i do this with awk? any ideas??
>
> regards
> dean
>


This:

BEGIN{ numFlds=split("10 8 25 8 34 6 64 10",flds) }
{ for (i=1;i<=numFlds;i++) { f[++nf]=substr($0,flds[i],flds[++i]) }

will create an array "f" of "nf" fields, so you'd then use "nf" like
you'd normally use "NF" and "f[1]" like you'd use "$1", etc. The string
passed to "split" is obviously a set of "position size" pairs and the
"f" array only contains the fields you're interested in.

Ed.
Sponsored Links







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

Copyright 2008 codecomments.com