Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View tabular file such as CSV from command line [closed]

Anyone know of a command-line CSV viewer for Linux/OS X? I'm thinking of something like less but that spaces out the columns in a more readable way. (I'd be fine with opening it with OpenOffice Calc or Excel, but that's way too overpowered for just looking at the data like I need to.) Having horizontal and vertical scrolling would be great.

like image 297
Benjamin Oakes Avatar asked Dec 09 '09 17:12

Benjamin Oakes


People also ask

Which of the following command is used to open a CSV file automatically?

system command is used to automatically open the CSV file.

How do I view a CSV file?

Answer: You can open the CSV file on Google Sheet, Notepad, or OpenOffice Calc. Just right-click on the file, select Open With and pick either OpenOffice Calc or Notepad. To open in Google Sheets, go to the File option in Google Sheet, click import, select the CSV file you want to open, click import.

How do I open a CSV file in Windows 10?

If you already have Microsoft Excel installed, just double-click a CSV file to open it in Excel. After double-clicking the file, you may see a prompt asking which program you want to open it with. Select Microsoft Excel. If you are already in Microsoft Excel, you can choose File > Open and select the CSV file.


2 Answers

You can also use this:

column -s, -t < somefile.csv | less -#2 -N -S 

column is a standard unix program that is very convenient -- it finds the appropriate width of each column, and displays the text as a nicely formatted table.

Note: whenever you have empty fields, you need to put some kind of placeholder in it, otherwise the column gets merged with following columns. The following example demonstrates how to use sed to insert a placeholder:

$ cat data.csv 1,2,3,4,5 1,,,,5 $ sed 's/,,/, ,/g;s/,,/, ,/g' data.csv | column -s, -t 1  2  3  4  5 1           5 $ cat data.csv 1,2,3,4,5 1,,,,5 $ column -s, -t < data.csv 1  2  3  4  5 1  5 $ sed 's/,,/, ,/g;s/,,/, ,/g' data.csv | column -s, -t 1  2  3  4  5 1           5 

Note that the substitution of ,, for , , is done twice. If you do it only once, 1,,,4 will become 1, ,,4 since the second comma is matched already.

like image 166
user437522 Avatar answered Oct 14 '22 18:10

user437522


You can install csvtool (on Ubuntu) via

sudo apt-get install csvtool 

and then run:

csvtool readable filename | view - 

This will make it nice and pretty inside of a read-only vim instance, even if you have some cells with very long values.

like image 35
d_chall Avatar answered Oct 14 '22 18:10

d_chall