need to save the space when converting to CSV file

Hi,

I have a text file with the following format. Some of the fields are blank.

1234    3456    23  45464  327837283232    343434
      5654353    34  34343    3434345          434242

....

....

....

I need to convert this file to a CSV file, like

1234,    ,23,  ,454,64,3278,3728,3232,    ,343,434

Here is my code:

inputFile="test"
outputFile="test.csv"
 
while read line
do
    f1=`echo $line | cut -c1-4`
    f2=`echo $line | cut -c5-8`
    f3..
    f4..
    .. 
    ..
    echo "$f1,$f2,$f3....." >> $outputFile
done < $inputFile

but in the outputFile, all the blank are erased. Can anyone help me out here?

Thanks!!:o

Please verify the input data, and be sure to wrap with CodeTags to maintain data integrity - spaces, tabs, etc...

I applied CodeTags to your original sample, but this does not look like it mapped correctly.

If these are fixed-width fields:

$ cat fsplit.awk

BEGIN {
        OFS=","
        # Big list of character ranges, split into A[1]="1-4", ...
        X=split("1-4,5-5,11-12,13-13,14-16,17-18,20-23,24-27,28-31,32-32,33-35,36-38", A, ",");
        for(N=1; N<=X; N++)
        {
                split(A[N], B, "-"); # Split "1-4" into B[1]="1", B[2]="4"
                delete A[N];
                S[N]=B[1]; # Keep the same
                E[N]=(B[2]-B[1])+1; # Calculate the length from the offset
        }
}

{
        STR=$0
        $0=""
        # Assemble a big list of fields from each given position, stripping
        # the appropriate section of string out of STR.
        for(N=1; N<=X; N++) $N=substr(STR, S[N], E[N]);

# print every line.
} 1

$ awk -f fsplit.awk data

1234, ,23, ,454,64,3278,3728,3232, ,343,434

$

I suspect the fields will be wrong for your data given the lack of code tags, but you can modify the numbers as you see fit. It's just

START1-END1,START2-END2,START3-END3,...

Corona688, thank you for your code!! I also fixed my code to work. I need to change:

f1=`echo $line | cut -c1-4`

to:

f1=`echo "$line" | cut -c1-4`

:o