leah blogs

July 2013

23jul2013 · Summer of Scripts: cmc

There are many tools to format narrow lines into multiple columns, for example Plan 9 mc, BSD rs or column:

% utter foo{1..16} | 9 mc
foo1  foo3  foo5  foo7  foo9  foo11 foo13 foo15
foo2  foo4  foo6  foo8  foo10 foo12 foo14 foo16
% utter foo{1..16} | rs  
foo1   foo3   foo5   foo7   foo9   foo11  foo13  foo15  
foo2   foo4   foo6   foo8   foo10  foo12  foo14  foo16  
% utter foo{1..16} | column
foo1    foo3    foo5    foo7    foo9    foo11   foo13   foo15
foo2    foo4    foo6    foo8    foo10   foo12   foo14   foo16

One problem with these tools is that a few long elements bloat the output:

% utter foo{1..5} foobarquuxmeh6 foo{7..16} | 9 mc
foo1           foo5           foo9           foo13
foo2           foobarquuxmeh6 foo10          foo14
foo3           foo7           foo11          foo15
foo4           foo8           foo12          foo16

My tool cmc is made for formatting such lists:

% utter foo{1..5} foobarquuxmeh6 foo{7..16} | cmc 
foo1    foo2    foo3    foo4    foo5    foobarquuxmeh6  foo7    foo8    foo9
foo10   foo11   foo12   foo13   foo14   foo15   foo16

Essentially, it aligns the contents into multiples of some column width, for example 6:

% ls / | cmc -t 6
afs   altboot     bin   boot  data  dev   dump  etc   home  lib   lib64
lost+found  mnt   opt   proc  root  run   sbin  service     srv   sys   tmp
usr   var

This format can be a bit quirky, but it’s still easier to scan than a completely unformatted list, and it takes far less space than a strict column layout.

NP: Die Schnitter—Nichstdestotrotz

Copyright © 2004–2018