Home > OS >  Filter csv with or and export new file
Filter csv with or and export new file

Time:07-20

i have a csv where i have to select a country and export a new csv only with the values of the country "de". I do it with this code. It works fine.

Import-Csv .\temp.csv -encoding utf8 |
where Country -Like 'de' | 
Export-Csv .\new-de.csv -encoding utf8 -NoTypeInformation

Now i need to include too the country code "at". Im struggling and couldnt get it solved how to use the -or. Like here (https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_logical_operators?view=powershell-7.2) have tried.

where Country -Like ('de' -or 'at') |

In PS ISE there is no error, but the export file is empty.

Have searched for similar use cases but could not solve it. Tried different things too with try and error without success.

Does anyone have an idea?

Regards, Hubertus

CodePudding user response:

Considering the column of countryes, until in the first row the title is named as 'Country' this should be the solution for you:

# import csv file
$csvFile = import-csv "csvFile.csv"
# export de and at records
$csvFile | Where-Object {($_.Country -Like 'de') -or ($_.Country -like 'at')} | Export-Csv .\new-country.csv -encoding utf8 -NoTypeInformation

hope will be userful

  • Related