Pretty Print a simple csv in the command line

python -c 'import sys,csv; c = csv.reader(sys.stdin); [sys.stdout.write("^M".join(map(repr,r))+"\n") for r in c];' <tmp/test.csv | column -s '^M' -t
Will handle pretty much all types of CSV Files. The ^M character is typed on the command line using Ctrl-V Ctrl-M and can be replaced with any character that does not appear inside the CSV. Tips for simpler CSV files: * If newlines are not placed within a csv cell then you can replace `map(repr, r)` with r
Sample Output
'Date'        'info'    'name'              'amount'
'3/4/2009'    'abc123'  'Momo, Chika'       '1,000.00'
'9/14/2008'   'abc123'  'Kilo, Bamya'       '500'
'2/21/2009'   'abc123'  'Abeer, Bilhaj'     '10,000'
'5/9/2008'    'abc123'  'Adel, Ayoob'       '100'
'10/10/2008'  'abc123'  'Merabet, Jinkins'  ''
'10/13/2008'  'abc123'  'tito, micheal'     '50'

1
By: pykler
2010-02-01 14:57:25

2 Alternatives + Submit Alt

  • Splits the input based on commas and prints it in a nice column format. This would not work for CSV rows that have "," between quotes or with newline characters. Use only simple simple csv files. Show Sample Output


    18
    column -s, -t <tmp.csv
    pykler · 2009-09-24 20:57:32 0

  • 0
    column -s, -t < somefile.csv | less -#2 -N -S
    DonDaniello · 2015-12-03 10:53:14 0

What Others Think

This version should work with all CSV files!
pykler · 458 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: