Awk print all columns in delimited file

text file example
1,222222222222,333,444444444444444
111111111,22,33333333,444

desired output

1                            222222222222 333       444444444444444
111111111                    22           33333333 444

I have a delimeted file which I want to print in a table like format. The command below is close however tab is not always big enough to properly space the columns?

awk  'BEGIN { FS=";"; OFS=" "} { $1=$1;print $0 }' deals_RBS.log

Any suggestions?

Thansk

I can't test it but this should work :

column -s ',' -t deals_RBS.log

Try

awk -F, '{for (i=1;i<=NF;i++) printf "%-15s",$i; print ""}' 

and adjust the field length (15) according to your needs.

Hi RudiC, that kind of works, but not really. Some columns are very short fileds i.e 1 or 2 chars, and some column are 20-25 chars wide.

If I increase -15 -> -30, the file goes over multiple lines and is unreadable.
Howerver if I keep it at -15, some columns are not seperated by anything.

Really what I need is to loop through each column of data and find the maximum width. I thought about cuting each colum and then pasting back together?

delugeag's "column" proposal does exactly what you require. I was aiming for columns of equal, constant width, and his proposal adapts to the respective neccessary width.

Hi Rudic,

I tried with delugeag's solution... getting below error and one line missing in output..

$ column -s ',' -t file2
column: line too long
1          222222222222  333       444444444444444
111111111  22            33333333  444
143        222222222222  33433     44444444444444434
12434      222222222222  333       444
14324      2222222222    3433      4444444

$ cat file2
1,222222222222,333,444444444444444
111111111,22,33333333,444
143,222222222222,33433,44444444444444434
12434,222222222222,333,444
14324,2222222222,3433,4444444
143,222222222,334333,44444444444

What could be the reason for this...?

Your file2 as posted works fine for me, even when I make lines four times as long. Sure you got no special chars in the original (not posted) file?

Got it: missing <newline> at the end of last line!

Thanks Rudic,

It works...:slight_smile:

Below is an awk solution but requires defining the maximum widths up front.

awk -F"," 'BEGIN {widthlist = "15 13 9 15"; split (widthlist,widths," ") } {for (i=1; i<=NF; i++) printf "%-*s", widths,$i ;printf "\n" }' deals_RBS.log
 
1              222222222222 333      444444444444444
111111111      22           33333333 444       
 

A perl solution :

perl -alnF, -e '{for($i=0;$i<=$#F;$i++){printf("%-18s",$F[$i]);}printf "\n";}' input_file 

Ok, i find this similar to one written by Rudi C though in awk :slight_smile:

Sorry for the late reply, columns worked a treat.

Thanks everyone for helping out