06. Column Editing and Pretty Printing column, colrm, fold

Pretty printing

The column command is the "pretty-print" of the command line. It formats its input into multiple columns.

-c
Specify how wide each column should be.
-s
Specify set of characters
-t
Determine number of columns the input contains and create a table. Columns delimited by whitespace or the characters specified with the -s option.
-x
Fill columns before filling rows.

Column removal

The colrm command removes a range of columns from standard in or a file.

$ cat birthdates.txt
05/24/1999,4
04/23/1974,2
08/09/2001,5
11/24/1991,3
01/23/1975,1
$ colrm 3 5 < birthdates.txt
# remove from column 3 to 5
05/1999,4
04/1974,2
08/2001,5
11/1991,3
01/1975,1
$ colrm 6 < birthdates.txt
# remove from column 6 onward.
05/24
04/23
08/09
11/24
01/23

Files with tabs

When working with files containing tabs, be sure to convert to spaces using the expand command first - otherwise, some unexpected behavior may occur.

Max-width with fold

The fold command makes long lines more readable by placing a new line every n characters.

To set n, use the -w option.

For example, take a look at a Lorem Ipsum text, which has no line breaks.

$ cat lorem.txt
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Curabitur dignissim venenatis pede. Quisque dui dui, ultricies ut, facilisis non, pulvinar non, purus. Duis quis arcu a purus volutpat iaculis. Morbi id dui in diam ornare dictum. Praesent consectetuer vehicula ipsum. Praesent tortor massa, congue et, ornare in, posuere eget, pede.

With the fold command we can break it up into lines with a width value of 45.

$ fold -w 45 lorem.txt
Lorem ipsum dolor sit amet, consectetuer adip
iscing elit. Curabitur dignissim venenatis pe
de. Quisque dui dui, ultricies ut, facilisis
non, pulvinar non, purus. Duis quis arcu a pu
rus volutpat iaculis. Morbi id dui in diam or
nare dictum. Praesent consectetuer vehicula i
psum. Praesent tortor massa, congue et, ornar
e in, posuere eget, pede.

This way, we are able to set a default width to our files for better readability.

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Inversion Therapy Table

Aching back from coding all day? Try Back Problems

Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.

$$ Check price
119.98119.98Amazon 4.5 logo(1,700+ reviews)

More Back Problems resources

Ad