column tool for printing lists in columns
Illustrating that no matter how long one has been doing something, there’s always something new to learn in August I discovered the column command, which Debian packages in the bsdextrautils package. It has been over 20 years after I started using Linux regularly, yet this really useful little tool has somehow never entered my awareness before.
I have a little script, which I wrote a while ago, that finds the existing tag and category names from my published posts to help me avoid duplication of tags via subtle variants (e.g. using the tag networking instead of network). Originally this script used tabs (\t) to separate the names it found, however as the list of tags (in particular) has grown this has become difficult to read. In looking for a solution, I came across the column command which I’d not seen before. In typical “do one thing and do it well” style (which I am huge fan of - simple building blocks that we can use, and reuse, to make complex systems), it does what it says on the tin and formats the list of things it receives on STDIN into columns on STDOUT.
I present the revised find-tags-and-categories.bash in full:
#!/bin/bash
heading() {
echo "-------------------------------"
echo $@
echo "-------------------------------"
}
grep_posts() {
grep -h "^$1: " _posts/* | sed "s/^$1: //; s/ /\n/g" | sort -u
}
heading tags
grep_posts tags | column
echo
heading categories
grep_posts categories | column
echo