General
cancel
Showing results for
Did you mean:

## CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Occasional Contributor

## CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Is there a command that will convert a COMMA DELIMITED FILE (CSV) to a fixed length file in UNIX?
8 REPLIES
Acclaimed Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

HI:

Given a file like this:

"a","b","c"
"aa","bb","cc"
"AaA","BbB","CcC"

I assume that (for whatever reason) you want to pad out with trailing spaces. Then you could do (for example to make 40-character records) is:

# perl -ple '\$_.= "-" x (40-length(\$_))' file

Regards!

...JRF...
Occasional Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Let me clarify my question.. Is there a command that will convert a COMMA DELIMITED FILE (CSV) to a fixed column length file in UNIX?
Acclaimed Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Hi (again):

> Let me clarify my question.. Is there a command that will convert a COMMA DELIMITED FILE (CSV) to a fixed column length file in UNIX?

What's wrong with the *command* I wrote?

Substitute whatever value you want for '40' to gain the desired fixed line length.

...JRF...
Occasional Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

I did try the command but it is not lining up each field.

Example: I have a file with 4 fields I want the starting position in the same position.

Current file:
12121,XXX,ZZZZZ,Z
1212,XX,ZZZZ,Z

The results I would like is:

12121,XXX,ZZZZZ,Z
1212 ,XX ,ZZZZ ,Z

Honored Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

so you want each column to be of fixed length? If so, are all columns the same length (ie, 4 columns each 20 wide), or can they be different (ie col1 is 10, col2 is 25 and so on...)?
Acclaimed Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Hi (again):

# perl -ne 'chomp;@F=split /,/;printf "%-12s,%-12s,%-12s\n",@F' file

Regards!

...JRF...

Acclaimed Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

>The results I would like is: 12121,XXX,ZZZZZ,Z

Typically if you have fixed fields, you don't need the commas.
Respected Contributor

## Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

You could try the command
'tr'

Otherwise as the master said
some handy oneliners
with perl or sed or awk.