[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: expand (1) not useful enough. How about expandest?



On 2010年08月26日 11:02, Zhang Weiwu wrote:
> I always want a tool that:
>
>     * Check the whole data file, for each column, find the widest cell.
>     * expand the data file, use widest cell for each column.
>     * it should know one ideograph counts two characters wide.
>   
With awk it also can be done, in fact one line:

$ printf "ab\tcde\nf\tgh\n" > /tmp/test.tsv
$ awk -F $'\t' 'FILENAME != "-" { for (i=1; i<=NF; i++) {max[i] = (length($i) > max[i] ? length($i) : max[i]); }} FILENAME == "-" {for (i=1; i<=NF; i++) { printf("%-" max[i] "s ", $i);;} print "";}' /tmp/test.tsv - < /tmp/test.tsv 
ab cde 
f  gh  



But my awk one-liner have the fault that is the Chinese ideograph is
counted as one character, not as two character-wide. I don't know a way
to fix that yet.


Reply to: