powershell - Filtering CSV file based on row value -
i have csv file such 1 shown below need filter based on values on specific column. cannot import-csv
, because file large , takes long time. managed use excel based solution found online extremely inefficient , takes hours script run.
sample.csv
:
a,1,2,3,4,5 b,1,a,b,c,d c,1,2,3,4 d,2,1,2,3 e,5,1,1,1 f,8,1,1,1
i output rows column 2 greater or equal 2. is:
output.csv
:
d,2,1,2,3 e,5,1,1,1 f,8,1,1,1
how more efficient solution problem developed?
try this:
get-content foo.csv | {[int]($_.split(',')[1]) -ge 2}
get-content read csv file 1 line @ time. command filter objects passed it. if condition inside evals $true object gets passed on down pipeline. in case, split line on comma, grab second field (zero-based index means index 1), cast int
, compare -ge (greater or equal to) 2. note in powershell, type coercion based on left hand side (lhs) of binary operator -ge
. therefore want make sure lhs of type int
comparing ints , not strings.
Comments
Post a Comment