by nkcmr · 5y ago · 0 comments
A lot of folks mentioning great projects/solutions that work the same but I love me some good unix piping action:
cat data.csv | xsv select 1,3 | q 'select * from - where col1 != "foo"'
(xsv: also invaluable on the terminal: https://github.com/BurntSushi/xsv) cat data.csv | sqlite3 -csv ':memory:' '.import /dev/stdin data' 'select col1, col3 from data where col1 != "foo"'
You can of course use the .import command directly on the file, but keeping it there to show piping. cut -d, -f1,3 data.csv | grep -v '^foo'
I'm sure there are some things where you need sql/q, but this isn't one of them, and there is a lot of utility in productive with bare POSIX tools.But CSV permits quoted fields that contain literal ','. So in that case, splitting on ',' would be incorrect.
< data.vnl vnl-filter -p thiscolumn,thatcolumn 'thiscolumn != "foo"'
If you don't strictly need csv, those tools are quite nice, and have a really friendly learning curve. Disclaimer: I'm the authorr -e "data.table::fread(data.csv)[col1 != "foo", .(1,3)]"
> data.table::fread("version_downloads.csv")[date == "foo", .(1,3)]
Error: character string is not in a standard unambiguous format
Execution halted
Dunno how to fix this now.https://www.rdocumentation.org/packages/data.table/versions/...
Or you can pass colClasses = 'character' as an argument to read everything as a string, but that will be much much slower.
fread also reads from stdin so you can pass bash commands into the first parameter instead of using bash to run r via R -e.
Full disclosure, I'm the author of 'xsv' and the thing I was most interested in here was performance.
And I have no idea which string to fix or how to even fix it. The failure mode here is not great.