Problem when formating a variable delimited file to fixed length 
Author Message
 Problem when formating a variable delimited file to fixed length

Hi,
The example says it all:

the inputfile is like this (the { is the field separator):

1{54{Pedro{1
2{957{Miguel{2

i'm using the following command to reforme it (need to add zeros on
the left of each numeric field if is needed to complete the output
format, and on the alfabetical fields i need to add spaces on the
right):

awk -F"{" '{printf("%03d%02d%-10.10s%03d\n",$1,$2,$3,$4)}' inputfile>
outputfile

the outputfile comes like this:
00154Pedro     001
002957Miguel    002

the problem is obviously with second field in the second record - 957
- when the input is larger than the output length format.

The question is how can i force the output length format, for example,
in order to 957 comes reformed like 95 in the output.

Thanks
Miguel Pinto



Sat, 02 Oct 2004 09:45:30 GMT  
 Problem when formating a variable delimited file to fixed length

Quote:

>Hi,
>The example says it all:

>the inputfile is like this (the { is the field separator):

>1{54{Pedro{1
>2{957{Miguel{2

>i'm using the following command to reforme it (need to add zeros on
>the left of each numeric field if is needed to complete the output
>format, and on the alfabetical fields i need to add spaces on the
>right):

>awk -F"{" '{printf("%03d%02d%-10.10s%03d\n",$1,$2,$3,$4)}' inputfile>
>outputfile

>the outputfile comes like this:
>00154Pedro     001
>002957Miguel    002

>the problem is obviously with second field in the second record - 957
>- when the input is larger than the output length format.

>The question is how can i force the output length format, for example,
>in order to 957 comes reformed like 95 in the output.

{printf("%03.3s%02.2s%-10.10s%03.3s\n",$1,$2,$3,$4)}

The idea is that you can cheat and use the string output identifier things,
and then the .n construct will truncate the string to that length.



Sat, 02 Oct 2004 09:56:10 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Convert comma-delimited records to fixed length records

2. problem ascii file with fixed record length

3. How to convert tab delimited file to space delimited file

4. need to format a variable delimited file to fixed length is AWK best choice?

5. Inserting First Field in Fixed Delimited File

6. Processing variable length/variable data files

7. Variable vs. fixed-length records performance

8. Variable vs. fixed length - tuning question

9. fixed or variable character length

10. Breaking up an asci or txt file with a fixed length

11. Creating fixed length text file

12. Binary search for fixed length ascii files.

 

 
Powered by phpBB® Forum Software