Aspersa User's Manual
The align tool
Aspersa Manual › The align tool

The align tool is a simple way to ensure that data is aligned in columns. It is written in Perl. It reads input from files specified on the command line, or from standard input.

Command-Line Options and Environment Variables

This tool has no options or environment variables.

How it Works

This tool works by reading the entire input into memory, and dividing each line into words. It then inspects the lines to derive the following information:

  • The maximum width of each column
  • The most frequent number of words in any line

It uses these to create a formatting specification based on the most frequent number of words, and then prints each conforming line with that format. Lines that don't have the right number of words are discarded.

The tool assumes that the first conforming line is a header line, and the second is the first line of "real" content. It formats columns as right-aligned if the value in the second row looks numeric, and left-aligned otherwise. Therefore, there must be at least two lines of input for the tool to produce any output.

The tool's behavior has some important consequences. Reading the entire input before formatting means that you can't use it for aligning data as it is generated incrementally, and you probably don't want to use this tool on very large files. Discarding lines with the wrong number of words means that some lines won't be printed.

Example Usage

An example of what this tool is useful for is aligning vmstat output, which tends to be printed in column widths that make it very hard to read by default. Here's an example of raw vmstat output. Notice how the CPU usage columns are misaligned, and the cache column is so wide that 'si' appears under the 'so' heading:

procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------ r b swpd free buff cache si so bi bo in cs us sy id wa st 3 0 288 12096016 84132 13156504 0 0 13 29 1 2 1 0 98 0 0 2 0 288 12095628 84132 13156504 0 0 0 0 104 600 0 0 100 0 0 2 0 288 12095740 84132 13156508 0 0 0 0 123 685 0 0 100 0 0 2 0 288 12095724 84132 13156508 0 0 0 0 101 585 0 0 100 0 0 2 0 288 12095716 84132 13156520 0 0 0 0 123 687 0 0 100 0 0 28 12 288 11961288 84192 13200020 0 0 44104 416 599 93820 55 8 36 1 0 8 1 288 11939828 84208 13209392 0 0 8856 157 211 79596 67 7 26 0 0 31 0 288 11920332 84232 13229696 0 0 19996 5 258 62769 65 7 29 0 0 25 1 288 11907768 84248 13246424 0 0 16156 5 254 45958 54 5 41 0 0

After processing with align, the output is much easier to read:

r b swpd free buff cache si so bi bo in cs us sy id wa st 3 0 288 12096016 84132 13156504 0 0 13 29 1 2 1 0 98 0 0 2 0 288 12095628 84132 13156504 0 0 0 0 104 600 0 0 100 0 0 2 0 288 12095740 84132 13156508 0 0 0 0 123 685 0 0 100 0 0 2 0 288 12095724 84132 13156508 0 0 0 0 101 585 0 0 100 0 0 2 0 288 12095716 84132 13156520 0 0 0 0 123 687 0 0 100 0 0 28 12 288 11961288 84192 13200020 0 0 44104 416 599 93820 55 8 36 1 0 8 1 288 11939828 84208 13209392 0 0 8856 157 211 79596 67 7 26 0 0 31 0 288 11920332 84232 13229696 0 0 19996 5 258 62769 65 7 29 0 0 25 1 288 11907768 84248 13246424 0 0 16156 5 254 45958 54 5 41 0 0

Note that the first line, which had the wrong number of words, has been deleted after processing.