
Question: How do I do fixed width field delimination?
Quote:
>Question
>I have a data file like this.
>-0.42482E+01 0.16340E+01 0.11249E+00-0.10674E+01-0.65335E-01
>-0.50695E+01 0.17885E+01 0.10441E+00-0.12052E+01-0.59437E-01
>-0.53613E+01 0.17977E+01 0.12962E+00-0.12072E+01-0.88039E-01
>-0.53939E+01 0.17116E+01 0.15930E+00-0.11348E+01-0.12201E+00
>-0.53621E+01 0.16105E+01 0.20307E+00-0.10422E+01-0.16868E+00
>-0.53028E+01 0.14997E+01 0.24864E+00-0.94124E+00-0.21613E+00
>-0.52325E+01 0.13810E+01 0.29522E+00-0.83384E+00-0.26315E+00
>-0.51592E+01 0.12591E+01 0.33995E+00-0.72588E+00-0.30664E+00
>-0.50856E+01 0.11343E+01 0.38128E+00-0.61832E+00-0.34471E+00
>-0.50141E+01 0.10084E+01 0.41825E+00-0.51342E+00-0.37638E+00
>-0.49462E+01 0.88175E+00 0.45003E+00-0.41202E+00-0.40094E+00
>-0.48827E+01 0.75483E+00 0.47621E+00-0.31477E+00-0.41820E+00
>-0.48243E+01 0.62776E+00 0.49659E+00-0.22196E+00-0.42826E+00
>-0.47712E+01 0.50064E+00 0.51115E+00-0.13368E+00-0.43149E+00
>-0.47235E+01 0.37357E+00 0.52008E+00-0.49893E-01-0.42840E+00
>-0.46813E+01 0.24675E+00 0.52371E+00 0.29502E-01-0.41969E+00
>It's 5 columns of data. Is there any way I can separate it into 5
>fields? My guess is via fixed delimited feature.. but i don't know if awk
>has one or how to do it if it does.
You can use gawk's FIELDWIDTHS variable. For example, try this:
gawk 'BEGIN{FIELDWIDTHS="12 12 12 12 12"} {$1=$1;print }' infile
or try this:
gawk 'BEGIN{FIELDWIDTHS="12 12 12 12 12"} {print $3, $4, $5}' infile
alternatively, if you can't use gawk, you could assign variables using
the substr function, for example:
awk '{a=$0;
f1=substr(a, 1,12);
f2=substr(a,13,12);
f3=substr(a,25,12);
f4=substr(a,37,12);
f5=substr(a,49,12);
print f1, f2, f3, f4, f5}' infile
Chuck Demas
Needham, Mass.
--
Eat Healthy | _ _ | Nothing would be done at all,
Die Anyway | v | That no one could find fault with it.