简体   繁体   中英

delete a CSV rows based on column value in Powershell

I receive a CSV File everyday that has information I need, but i have to filter rows base on a different criteria for each row.

eg csv.

Name,Age,Date,Code
John,22,12/12/2020,5
Smith,16,12/01/2021,2
Jake,27,1/01/2020,1
Alice,18,2/02/2019,4
Fort,27,2/09/2021,2
Kevin,26,22/09/2019,5

I would like:

  1. Remove all ages 18 and below in column 2
  2. remove any dates past 2020 in column 3
  3. Remove any codes that are 5 in column 5

My Result should be

Name,Age,Date,Code
Jake,27,1/01/2020,1
Fort,27,2/09/2021,2
$csv = @"
Name,Age,Date,Code
John,22,12/12/2020,5
Smith,16,12/01/2021,2
Jake,27,1/01/2020,1
Alice,18,2/02/2019,4
Fort,27,2/09/2021,2
Kevin,26,22/09/2019,5
"@ | ConvertFrom-Csv

$csv.where{
    ($_.Age -le 18,
    [datetime]::ParseExact($_.Date, 'd/MM/yyyy', $null) -ge [datetime]::ParseExact('01/01/2020', 'dd/MM/yyyy', $null),
    $_.Code -eq 5)
}

Try it online!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM