Fixed length to delimited file conversion

Hi All,

I need to convert a fixed length file to a delimited file with , (comma). But not all columns, some of the columns in the fixed files are used as fillers and I do not need that in the output file.

test_fixed_len.txt

I   0515 MR       394 
I   0618 MR & MRS 942 
I   0618 MR & MRS 944 
I   0205 MR & MRS 614 
I   0122 MR       940 

The file is defined as

transaction type - 1 char
filler - 3 char
date - 4 char
filler - 1 char
name header - 8 char
filler 1
transaction number - 3 char

I need the input file converted into a delimited file like

I,0515,MR,394 
I,0618,MR & MRS,942 
I,0618,MR & MRS,944 
I,0205,MR & MRS,614 
I,0122,MR,940 

Which is:

transaction type,date,name header,transaction number

with no fillers.

I tried:

sed -e 's/./&,/1' -e 's/./&,/9' -e 's/./&,/19' test_fixed_len.txt

This is adding , in the positions I want. I also need to remove the extra spaces in a column if there are any.

Can anyone help me if there is a better way?

How about using awk instead?

awk  '{print substr($0,1,1), substr($0,5,4), substr($0,10,8), substr($0,19)}' OFS=, test_fixed_len.txt
1 Like

Thanks Yoda. This is giving me the csv file with the columns I need.

I,0515,MR      ,394
I,0618,MR & MRS,942
I,0618,MR & MRS,944
I,0205,MR & MRS,614
I,0122,MR      ,940

Is there a way I can trim the spaces from the fields to get something like this instead?

I,0515,MR,394
I,0618,MR & MRS,942
I,0618,MR & MRS,944
I,0205,MR & MRS,614
I,0122,MR,940

Try

sed 's/./,/4;s/./,/9;s/./,/18;s/ *,/,/g' file
I,0515,MR,394 
I,0618,MR & MRS,942 
I,0618,MR & MRS,944 
I,0205,MR & MRS,614 
I,0122,MR,940
1 Like

Thanks Rudi. One problem with this is, some of the columns are adjoining without fillers in between. For such cases this is not working.

GNU awk:

awk '{sub(/ *$/,x,$5); print $1,$3,$5,$7}' FIELDWIDTHS="1 3 4 1 8 1 3" OFS=, file
I,0515,MR,394
I,0618,MR & MRS,942
I,0618,MR & MRS,944
I,0205,MR & MRS,614
I,0122,MR,940
2 Likes

OK. Why didn't you specify that in the first place?

Try

sed 's/./&,/18;s/./&,/9;s/./&,/4;s/ *,/,/g' file
1 Like

Sry for the confusion. I picked up the first part of my test file which happened to have delimiters between fields.

Thanks for the help RudiC and Scrutinizer. Both codes are working.