Delimted to padded conversion with unknown field length

I�m looking for an elegant way to convert a delimited file (comma delimited in this case) to padded columns (for printing in non-proportional font) but the length of each column is not known ahead of time. It needs to be calculated for each column from the longest entry in that column in a given file.

I can live with a known number of columns, but I�d rather have something that does not rely on that.

I�d rather not brute force this (with a pre-read and column length arrays) if I�m missing some elegant use of an existing command.

My shell is BASH. This is for a Cygwin in a Windows environment (no choice).

Mike

Hi,

for a file like:

Year,Make,Model,Extras
1997,Ford,E350,variables
2000,Mercury,Cougar,examples

This command:

awk -F, 'NR==FNR{for (i=1;i<=NF;i++)
    {t=length($i)+2;(t>a)?a=t:a=a}}
    NR!=FNR{for (i=1;i<=NF;i++)
    {printf "%-"a"s", $i};printf "\n"}' file file

gives you:

Year  Make     Model   Extras     
1997  Ford     E350    variables  
2000  Mercury  Cougar  examples   

HTH Chris

1 Like

Thank you. Awk syntax looks a lot like C.

I think I understand what you are doing. You are using one program when it is the first run and another when it is the second. I'm trying to modify the script to ignore the length when there is only a single column on the line but I don�t understand the Awk language enough.

This is a long table header that is not data.
Year,Make,Model,Extras
1997,Ford,E350,variables
2000,Mercury,Cougar,examples

And I want:

This is a long table header that is not data.
Year  Make     Model   Extras     
1997  Ford     E350    variables  
2000  Mercury  Cougar  examples   

Instead of:

This is a long table header that is not data.
Year                                           Make     Model   Extras     
1997                                           Ford     E350    variables  
2000                                           Mercury  Cougar  examples   

Mike

Not pretty, but it works. It uses NF to check if the number
of fields is greater than 1.

awk -F, 'NF > 1 && NR==FNR{for (i=1;i<=NF;i++)
    {t=length($i)+2;(t>a)?a=t:a=a}}
    NR!=FNR{if (NF>1) {for (i=1;i<=NF;i++)
    {printf "%-"a"s", $i};printf "\n"}
    else print $0}' file file