Text-Columns

Dr. Andrew Broad
Computer Science
Java
Text-Columns


I often send raw text-files directly to the printer, and narrow files (with a small maximum line-length) can waste paper. One solution to this is to set up a word-processor document with two or more columns, import the text-file and print it from the word-processor, but I have written a program to reorganise a narrow text-file into columns much more quickly.

Text-Columns is a gorgeous Java program for `columnising' text-files. You specify the number of columns, and the width (in characters) of each column, and it creates a new text-file, with the text rearranged into columns, padded out with spaces (this assumes a fixed-width printer-font).

Text-Columns has a simple command-line interface - no faffing around with pointless GUIs for it! ;-) Say you wanted to columnise a text-file called counts.txt into four columns of 10 characters each - the following command-line would do the job:

java Columns count.txt 4 10
one       five      nine      thirteen  
two       six       ten       fourteen  
three     seven     eleven    fifteen   
four      eight     twelve    

The column-width cannot be smaller than the longest line in the input-file. Thus the minimum column-width (enforced by the program) for the above example is 8.

Obviously the column-width times the number of columns has to be less than or equal to the width at which your printer prints text-files, otherwise your printer will horizontally truncate the output-file, or perhaps use ugly word-wrap on overlength lines (you know your own printer).

If the file is more than a page long after columnising, then each column will span all of the pages, and the next column will start on the first page. But you probably want each column to start on the same page as the previous column, only going onto a new page when the current page is full. To achieve this, I've added an optional extra command-line argument to specify the number of lines on each page. So if you know your printer prints 64 lines per page, for example, you could use the following command:

java Columns long_count.txt 4 10 64 > ~/LASERJET2/count.txt
(The program writes its output to the standard output stream, so the above command redirects it to a file, which can subsequently be printed. Do not attempt to overwrite the input-file with the output-file, as this will wipe the input-file and leave a 0-length file.)


Email me
Hosted by www.Geocities.ws

1