Splitting a variable number of fixed length fields

Splitting a variable number of fixed length fields

Post by Al Shark » Mon, 06 Nov 2000 04:00:00




} I have a file that has a variable number of fixed length fields, eg:
}
} F1F2F3....F125
}
} Field 1 is always 120 bytes
} Field 2 up to Field 125 is always 60 bytes
}
} I need to split F2..F125 so that they are on a line by themselves, eg:
}
} F1
} F2
} F3
} ..
} F125
}
} I was thinking that it could be done with sed or awk (I could do it in Perl,
} but I'm keen to leave that option for the moment).

You are sure to run into line length limits with generic awk or sed,
as you have a possible length of 7560 bytes total.  HP awk chokes after
3000 bytes.

gnu awk doesn't seem to care.  This seems simple, maybe I missed the point?:

gawk '{print substr($0,1,120);
       for (i=121;i<length;i+=60) print substr($0,i,60);
      }' file

 
 
 

Splitting a variable number of fixed length fields

Post by Peter Sundstro » Tue, 07 Nov 2000 07:33:26


I have a file that has a variable number of fixed length fields, eg:

F1F2F3....F125

Field 1 is always 120 bytes
Field 2 up to Field 125 is always 60 bytes

I need to split F2..F125 so that they are on a line by themselves, eg:

F1
F2
F3
..
F125

I was thinking that it could be done with sed or awk (I could do it in Perl,
but I'm keen to leave that option for the moment).

Any pointers greatfully accepted.

 
 
 

Splitting a variable number of fixed length fields

Post by bmar.. » Tue, 07 Nov 2000 04:00:00



   >I have a file that has a variable number of fixed length fields, eg:
   >F1F2F3....F125
   >Field 1 is always 120 bytes
   >Field 2 up to Field 125 is always 60 bytes
   >I need to split F2..F125 so that they are on a line by themselves,
   >eg:
   >F1
   >F2
   >F3
   >...
   >F125
   >I was thinking that it could be done with sed or awk (I could do it
   >in Perl, but I'm keen to leave that option for the moment).
   >Any pointers greatfully accepted.
You could use gawk with its FIELDWIDTHS array, or in regular awk:
awk '{print substr($0,1,120)
 for (x=121;x<length;x+=60) print substr($0,x,60)}' infile > outfile

Net-Tamer V 1.08X - Test Drive

 
 
 

Splitting a variable number of fixed length fields

Post by Peter Sundstro » Wed, 08 Nov 2000 06:22:17




> } I have a file that has a variable number of fixed length fields, eg:
> }
> } F1F2F3....F125
> }
> } Field 1 is always 120 bytes
> } Field 2 up to Field 125 is always 60 bytes
> }
> } I need to split F2..F125 so that they are on a line by themselves, eg:
> }
> } F1
> } F2
> } F3
> } ..
> } F125
> }
> } I was thinking that it could be done with sed or awk (I could do it in
Perl,
> } but I'm keen to leave that option for the moment).

> You are sure to run into line length limits with generic awk or sed,
> as you have a possible length of 7560 bytes total.  HP awk chokes after
> 3000 bytes.

> gnu awk doesn't seem to care.  This seems simple, maybe I missed the
point?:

> gawk '{print substr($0,1,120);
>        for (i=121;i<length;i+=60) print substr($0,i,60);
>       }' file

Thanks for this solution.  It works well.

However, I've decided to go down the Perl path after all.  The Perl solution
is about twice as quick as the awk solution.

cat file | perl -e 'print join("\n", m/^.{120}|\G.{60}|.+$/g) . "\n" while
(<>);'

 
 
 

Splitting a variable number of fixed length fields

Post by Peter Sundstro » Thu, 09 Nov 2000 06:57:43



Quote:>Oops, a slight change, in the line "offset=121+(a*60)" ie now 121 instead
of 120
>#!/bin/nawk -f
>{
>len=length($0)
>numflds=(len-120)/60
>printf("%120s\n",substr($0,1,120))
>for(a=0;a<numflds;a++){
>       offset=121+(a*60)
>        printf("%60s\n",substr($0,offset,60))
>}
>}

Thanks for the suggestion Reg, but I ended up using a Perl solution as it was twice as quick as awk.