GNU/Linux Desktop Survival Guide
by Graham Williams |
|||||
CSV Files |
20200421 The command mlr from the package miller is a powerfull command line tool for working with csv, tsv, and tabular JSON files.
A verb is required to specify an action. Here we simply cat the file:
$ mlr cat example.csv 1=color,2=shape,3=flag,4=index,5=quantity,6=rate 1=yellow,2=triangle,3=1,4=11,5=43.6498,6=9.8870 ... |
Note that the input is a csv file using –icsv:
$ mlr --icsv cat example.csv color,shape,flag,index,quantity,rate yellow,triangle,1,11,43.6498,9.8870 red,square,1,15,79.2778,0.0130 ... |
Pretty print the output using –opprint:
$ mlr --icsv --opprint cat example.csv color shape flag index quantity rate yellow triangle 1 11 43.6498 9.8870 red square 1 15 79.2778 0.0130 ... |
Select fields from the file using the verb cut:
$ mlr --csv --opprint cut -f flag,shape example.csv shape flag triangle 1 square 1 ... |
Order the fields as specified:
$ mlr --icsv --opprint cut -o -f flag,shape example.csv flag shape 1 triangle 1 square ... |
Select rows using the verb filter with both input and output as csv:
$ mlr --csv filter '$flag == 1' example.csv color,shape,flag,index,quantity,rate yellow,triangle,1,11,43.6498,9.8870 red,square,1,15,79.2778,0.0130 |
Select rows using the verb filter on a character field:
$ mlr --csv filter '$color == "yellow"' example.csv color,shape,flag,index,quantity,rate yellow,triangle,1,11,43.6498,9.8870 |
Further details at https://johnkerl.org/miller/doc/.