Sorting columns of text in Vim using sort
I recently wanted to get some stats on some Mongo collections to see a) which collection had the most documents; and b) which collections were the biggest by data size.
I copy and pasted the stats from our hosted Mongo provider, MongoHQ, and then
sorted them in Vim. This is what the text looked like after pasting into Vim
(with the :set paste
option set).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
First I sorted the text into "proper" columns using the column
utility.
1
:%!column -t
Which resulted in nice, spaced out, columns.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
To sort the text by the total number of documents in the collection, I did this.
1
:%!sort -k2nr
This sorted by the second column (-k2
), treats the text as a number (n
) and
then sorts in reverse (r
), which results in.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
page_views 107769 16.37 MB
recommendations 34305 45.1 MB
versions 18031 156.64 MB
sign_ups 10331 12.52 MB
fs.chunks 3401 633.89 MB
fs.files 1476 680 KB
nodes 1432 24.29 MB
tariff_price_checks 1129 968 KB
affiliates 1038 680 KB
article_ratings 699 168 KB
nodes_tags 272 40 KB
page_views_map 212 40 KB
rewrite_rules 209 168 KB
nodes_search 91 2.8 MB
tariff_price_check_reports 34 540 KB
authors 30 40 KB
users 17 64 KB
suppliers 13 8 KB
tariffs 5 680 KB
users_tags 2 8 KB
sitemaps 1 14.84 MB
Then, I sort by the the 4th column (-k4
), followed by the 3rd column, but this
time we require a few more switches. We ignore leading blank spaces (b
), and
this time we sort using a general numeric sort (g
).
1
:%!sort -k4 -bk3g
And there we have it, sorted by file size.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
*nix is cool.