yumebayashi's note

q command for querying on csv file directly

https://github.com/harelba/q

environment

requires python 2.X
brew install q

usage

you can read help by -h option.
q -h

if csv has header, we need append -H.

q -d , -H "select * from tmp.csv"
1,aaa,23
2,bbb,54
3,ccc,12
4,ddd,65
5,eee,33
6,fff,75
7,ggg,80
8,hhh,89
9,iii,34
10,kkk,96
q -d , -H "select id from tmp.csv order by id desc limit 4"
10
9
8
7
q -d , -H "select *,(case when score > 50 then 1 else 0 end) from tmp.csv" > tmp2.csv
1,aaa,23,0
2,bbb,54,1
3,ccc,12,0
4,ddd,65,1
5,eee,33,0
6,fff,75,1
7,ggg,80,1
8,hhh,89,1
9,iii,34,0
10,kkk,96,1

we choose column like c1,c2 for no-header files

q -d , "select c4, sum(c3) from tmp2.csv group by c4"
0,102
1,459

below query returns the same result as above. we can pipe the stdout with "-"

q -d , -H "select *,(case when score > 50 then 1 else 0 end) from tmp.csv" | q -d , "select c4, sum(c3) from -  group by c4"
0,102
1,459