howl.moe

cutcsv: the csv swiss army knife

Published (click to see context) on 18/03/2021 by Morgan Bazalgette • 1 minute
$ cutcsv -h
cutcsv - the csv swiss army knife (version 0.1.0)

Usage: cutcsv OPTION... [FILE]
Print selected CSV fields from each specified FILE to standard output.
With no FILE, or when FILE is -, read standard output.

    -f <LIST>   Print the specified comma-separated list of fields or ranges (required)
    -d <CHAR>   Use CHAR as a delimiter (defaults to ',')
    -D <STRING> Use STRING as the output separator (in case of multiple fields).
                Defaults to the input delimiter.
    -v          Be verbose

$ echo "a,b,c\nd,e,f\ng,h,\"i\"" | cutcsv -f2,3
b,c
e,f
h,i

To my incredible surprise, after a quick look on Google it seemed nobody already had my idea: making a cut like interface to deal with CSV files. (Of course, you can always just do cut -d, -f1 - but this does not work properly with quotes.) The idea is quite simple: add support for quotes so that we can use UNIX tools on pretty much any CSV file.

You can find it here: https://zxq.co/f4/cutcsv
It has, obviously, no dependencies and provides a handy build script using tool build. There are also build artifacts containing the binaries.

#blogpost #dev