General
cancel
Showing results for 
Search instead for 
Did you mean: 

CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

MBacc
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
James R. Ferguson
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...
MBacc
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?
James R. Ferguson
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...
MBacc
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

OldSchool
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...)?
James R. Ferguson
Acclaimed Contributor

Re: CONVERTING A COMMA DELIMITED FILE TO FIXED LENGTH FILE

Hi (again):

Oh, OK. How about this (adjust the field widths as necessary):

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

Regards!

...JRF...

Dennis Handly
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.
Frank de Vries
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.

google is your best friend :°)

Look before you leap