Formatted CSV output on Linux shell

If you want to output a CSV file on Linux shell, you could just open it with less, cat or nano (un-formatted CSV output):

The problem with this approach is, that you will get an unformatted ugly chunk of characters like that:

View CSV file in shell (un-formatted CSV output)
View CSV file in shell un-formatted

A much better way is it, to use the less command in combination with the column command like that:

With the -s param you can define the CSV delimiter. In our case this is a semicolon (‘;’).

The -t parameter we tells column to make a table out of the fields separated by whitespaces (default) or – in our case – the delimiter specified by -s.

So column opens the given file and splits the content into columns. Afterwards the splittet content is sent to less via a pipe (|).

The -N param causes line numbers for each output row.

-S forces long lines to continue. That’s why theses lines do not fold and the content became scrollable to the right and left.

And finally -# 4 specifies, that the output should be moved by 4 positions (characters) on each keystroke to the left- or right arrow key (or kept pressed).

With this approach, you will get a nicely formatted output like that (formatted CSV output):

View CSV file in shell (formatted CSV output)
View CSV file in shell formatted

Which is much better to read. And you can just scroll from left to right and the other way around by using the arrow keys.

Leave a Reply

Your email address will not be published. Required fields are marked *

 

This site uses Akismet to reduce spam. Learn how your comment data is processed.