A fast CSV command line toolkit written in Rust.
xsv is now unmaintainedIn lieu of xsv, I'd recommend either
qsv
or
xan.
xsv is a command line program for indexing, slicing, analyzing, splitting and joining CSV files. Commands should be simple, fast and composable:
This README contains information on how to
install xsv, in addition to
a quick tour of several commands.
Dual-licensed under MIT or the UNLICENSE.
xsv slice -i 5 data.csv | xsv flatten.Let's say you're playing with some of the data from the Data Science Toolkit, which contains several CSV files. Maybe you're interested in the population counts of each city in the world. So grab the data and start examining it:
$ curl -LO https://burntsushi.net/stuff/worldcitiespop.csv
$ xsv headers worldcitiespop.csv
1 Country
2 City
3 AccentCity
4 Region
5 Population
6 Latitude
7 LongitudeThe next thing you might want to do is get an overview of the kind of data that
appears in each column. The stats command will do this for you:
…The xsv table command takes any CSV data and formats it into aligned columns
using elastic tabstops. You'll
notice that it even gets alignment right with respect to Unicode characters.
So, this command takes about 12 seconds to run on my machine, but we can speed it up by creating an index and re-running the command:
$ xsv index worldcitiespop.csv
$ xsv stats worldcitiespop.csv --everything | xsv table
...Which cuts it down to about 8 seconds on my machine. (And creating the index takes less than 2 seconds.)
Notably, the same type of "statistics" command in another CSV command line toolkit takes about 2 minutes to produce similar statistics on the same data set.
Creating an index gives us more than just faster statistics gathering. It also makes slice operations extremely fast because only the sliced portion has to be parsed. For example, let's say you wanted to grab the last 10 records:
…These commands are instantaneous because they run in time and memory proportional to the size of the slice (which means they will scale to arbitrarily large CSV data).
Switching gears a little bit, you might not always want to see every column in the CSV data. In this case, maybe we only care about the country, city and population. So let's take a look at 10 random rows:
$ xsv select Country,AccentCity,Population worldcitiespop.csv \
| xsv sample 10 \
| xsv table
Country AccentCity Population
cn Guankoushang
za Klipdrift
ma Ouled Hammou
fr Les Gravues
la Ban Phadèng
de Lüdenscheid 80045
qa Umm ash Shubrum
bd Panditgoan
us Appleton
ua LukashenkivskeWhoops! It seems some cities don't have population counts. How pervasive is that?
…(The xsv frequency command builds a frequency table for each column in the
CSV data. This one only took 5 seconds.)
So it seems that most cities do not have a population count associated with them at all. No matter—we can adjust our previous command so that it only shows rows with a population count:
$ xsv search -s Population '[0-9]' worldcitiespop.csv \
| xsv select Country,AccentCity,Population \
| xsv sample 10 \
| xsv table
Country AccentCity Population
es Barañáin 22264
es Puerto Real 36946
at Moosburg 4602
hu Hejobaba 1949
ru Polyarnyye Zori 15092
gr Kandíla 1245
is Ólafsvík 992
hu Decs 4210
bg Sliven 94252
gb Leatherhead 43544Erk. Which country is at? No clue, but the Data Science Toolkit has a CSV
file called countrynames.csv. Let's grab it and do a join so we can see which
countries these are:
…Whoops, now we have two columns called Country and an Abbrev column that we
no longer need. This is easy to fix by re-ordering columns with the xsv select command:
…Perhaps we can do this with the original CSV data? Indeed we can—because
joins in xsv are fast.
…The !Abbrev,Country[1] syntax means, "remove the Abbrev column and remove
the second occurrence of the Country column." Since we joined with
countrynames.csv first, the first Country name (fully expanded) is now
included in the CSV data.
This xsv join command takes about 7 seconds on my machine. The performance
comes from constructing a very simple hash index of one of the CSV data files
given. The join command does an inner join by default, but it also has left,
right and full outer join support too.
Binaries for Windows, Linux and macOS are available from Github.
If you're a macOS Homebrew user, then you can install xsv from homebrew-core:
$ brew install xsvIf you're a macOS MacPorts user, then you can install xsv from the official ports:
$ sudo port install xsvIf you're a Nix/NixOS user, you can install xsv from nixpkgs:
$ nix-env -i xsvAlternatively, you can compile from source by
installing Cargo
(Rust's package manager)
and installing xsv using Cargo:
cargo install xsvCompiling from this repository also works similarly:
git clone git://github.com/BurntSushi/xsv
cd xsv
cargo build --releaseCompilation will probably take a few minutes depending on your machine. The
binary will end up in ./target/release/xsv.
I've compiled some very rough
benchmarks of
various xsv commands.
Here are several valid criticisms of this project:
xsv with
more sophisticated indexing support. And the performance is a zillion times
better.I'm sure there are more criticisms, but the impetus for this project was a 40GB CSV file that was handed to me. I was tasked with figuring out the shape of the data inside of it and coming up with a way to integrate it into our existing system. It was then that I realized that every single CSV tool I knew about was woefully inadequate. They were just too slow or didn't provide enough flexibility. (Another project I had comprised of a few dozen CSV files. They were smaller than 40GB, but they were each supposed to represent the same kind of data. But they all had different column and unintuitive column names. Useful CSV inspection tools were critical here—and they had to be reasonably fast.)
The key ingredients for helping me with my task were indexing, random sampling, searching, slicing and selecting columns. All of these things made dealing with 40GB of CSV data a bit more manageable (or dozens of CSV files).
Getting handed a large CSV file once was enough to launch me on this quest. From conversations I've had with others, CSV data files this large don't seem to be a rare event. Therefore, I believe there is room for a tool that has a hope of dealing with data that large.
This project is unrelated to another similar project with the same name: https://mj.ucw.cz/sw/xsv/
Looking for maintainership
xsv fixlengths never finishes on some input
cargo install fails
feature request: slicing from end
Transparent decompression
Use some kind of BigInt for stats?
Frequency command causes allocated memory exhausted message on csv file with many fields
Add Parquet support
sort by mixed column types, some numeric and some non-numeric, some forward and some reverse
`xsv table` has unexpected output with long input (too-wide tabs, missing tabs)