Search code examples
csvkit

How to fillter a CSV file with csvgrep and print only certain columns?


Is it possible in a single invocation, or do I need to reparse the lines with a pipe?

E.g. given:

test.csv

ok,1
ok,2
ko,3

and what I want is only the second column if the first column is ok, i.e. the desired output is:

1
2

The only way I know how to do it is with a pipe into csvcut:

csvgrep -H -c1 -mok test.csv | csvcut -c2 | tail -n+2

This example from the documentation: https://csvkit.readthedocs.io/en/latest/tutorial/2_examining_the_data.html#csvgrep-find-the-data-you-need does something similar, so maybe it is the only way?

csvcut -c county,item_name,total_cost data.csv | csvgrep -c county -m LANCASTER | csvlook

Solution

  • Using Miller you can run

    mlr --csv -N filter '$1=="ok"' then cut -f 2 input.csv
    

    to get

    1
    2
    

    As input I have used

    ok,1
    ok,2
    ko,3