Command-Line CSV Viewer

Article by:
Date Published:
Last Modified:
  1. Create a new file called csv in /usr/bin/ (or in /usr/bin/local/ if it exists in your distro):

    1
    
    $ sudo vim /usr/bin/csv
    
  2. Paste the following code into the file (credit goes to Benjamin Noakes for this code):

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    #!/usr/bin/env bash
    # Author: Benjamin Oakes <[email protected]>
    set -o errexit
    
    function show_usage {
      cat <<EOF
    Usage: $0 [--help] [filename]
    View a CSV file at the command line.
      --help        Show this help text.
      filename      CSV file to be viewed
    EOF
      exit -1
    }
    
    if [ "$1" == "--help" -o "$1" == "" ]; then
      show_usage
    fi
    
    cat "$1" | column -s, -t | less -#2 -N -S
    
  3. Exit vim (good luck!). Make this script executable:

    1
    
    $ sudo chmod +x ./csv
    
  4. All done! As long as /usr/bin/ is on your PATH, you should be able to nicely display CSV files in the command line with:

    1
    
    $ csv my_file.csv
    

Authors

Geoffrey Hunter

Dude making stuff.

Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License .

Related Content:

Tags

comments powered by Disqus