jordelver
jordelver

Reputation: 8432

How can I sort columns of text in Vim by size?

Given this text

affiliates                  1038    680     KB
article_ratings             699     168     KB
authors                     30      40      KB
fs.chunks                   3401    633.89  MB
fs.files                    1476    680     KB
nodes                       1432    24.29   MB
nodes_search                91      2.8     MB
nodes_tags                  272     40      KB
page_views                  107769  16.37   MB
page_views_map              212     40      KB
recommendations             34305   45.1    MB
rewrite_rules               209     168     KB
sign_ups                    10331   12.52   MB
sitemaps                    1       14.84   MB
suppliers                   13      8       KB
tariff_price_check_reports  34      540     KB
tariff_price_checks         1129    968     KB
tariffs                     5       680     KB
users                       17      64      KB
users_tags                  2       8       KB
versions                    18031   156.64  MB

How can I sort by the 4th and then 3rd column so that it's sorted by file size?

I've tried :%!sort -k4 -k3n which partially works, but seems to fail on the 3rd size column.

What am I doing wrong?

Upvotes: 0

Views: 244

Answers (2)

jordelver
jordelver

Reputation: 8432

I think I've figured it out.

:%!sort -k4 -bk3g

I sort by the the 4th column (-k4), followed by the 3rd column. We ignore leading blank spaces (b), and this time we sort using a general numeric sort (g).

I blogged about this too

Upvotes: 1

Birei
Birei

Reputation: 36272

I don't know how to handle it with sort(). I've found problems with the decimal point, although I changed the LC_NUMERIC environment variable, so I would switch to to solve it, like:

:%!perl -0777 -ne '
    @l = map { [ $_, split " ", $_ ] } split /\n/, $_; 
    @l = sort { $a->[-1] cmp $b->[-1] or $a->[-2] <=> $b->[-2] } @l; 
    print "$_->[0]\n" for @l
'

Put it in the same line to run if from inside . It yields:

suppliers                   13      8       KB
users_tags                  2       8       KB
authors                     30      40      KB
nodes_tags                  272     40      KB
page_views_map              212     40      KB
users                       17      64      KB
article_ratings             699     168     KB
rewrite_rules               209     168     KB
tariff_price_check_reports  34      540     KB
affiliates                  1038    680     KB
fs.files                    1476    680     KB
tariffs                     5       680     KB
tariff_price_checks         1129    968     KB
nodes_search                91      2.8     MB
sign_ups                    10331   12.52   MB
sitemaps                    1       14.84   MB
page_views                  107769  16.37   MB
nodes                       1432    24.29   MB
recommendations             34305   45.1    MB
versions                    18031   156.64  MB
fs.chunks                   3401    633.89  MB

Upvotes: 0

Related Questions