19.2 CSV Filter Rows

20210128

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
red,circle,1,16,13.8103,2.9010
yellow,circle,1,73,63.9785,4.2370
yellow,circle,1,87,63.5058,8.3350

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
yellow,circle,1,73,63.9785,4.2370
yellow,circle,1,87,63.5058,8.3350

Select rows using multiple filters (to limit the output):

$ mlr --csv filter '$color == "yellow"' then filter 'NR < 2' example.csv
color,shape,flag,index,quantity,rate
yellow,triangle,1,11,43.6498,9.8870

We can also ignore specific columns:

$ mlr --csv filter '$color == "yellow"' then put 'unset $index' example.csv
color,shape,flag,quantity,rate
yellow,triangle,1,43.6498,9.8870
yellow,circle,1,63.9785,4.2370
yellow,circle,1,63.5058,8.3350


Your donation will support ongoing availability and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984. Copyright © 1995-2022 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0