Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell filter csv

Tags:

powershell

csv

Looking for help on best practice as I am Powershell newbie,

I have a csv file and I would like to filter out every row in the csv except for the rows containing "Not Installed"

I would then like to filter those results against a separate csv files housing a list of computers and also exclude any rows containing a match.

Any help or starting points would be appreciated !

like image 774
user2284702 Avatar asked Apr 16 '13 04:04

user2284702


2 Answers

For the first part, you can use Select-String -Notmatch ... e.g.:

$file = "data.csv"
$csv = Get-Content $file
$csv | Select-String -Notmatch 'Not Installed' | Out-File $file -Enc ascii

If your CSV file happens to be Unicode, then remove the "-Enc ascii" part since the default for Out-File is Unicode. As for the second part of your question, you're going to need to be bit more specific. However, you should look at the help on Import-Csv and Export-Csv:

man Import-Csv -full
man Export-Csv -full
like image 160
Keith Hill Avatar answered Sep 28 '22 19:09

Keith Hill


get-help *csv*

get-help import-csv -examples

example csv file called test.txt

header1,header2,header3
installed,2,3
not installed,2,3

Import the file into a powershell variable and filter it.

$p = Import-CSV .\test.txt
$p | Where { $_.header1 -eq 'installed'}
like image 33
Paul Rowland Avatar answered Sep 28 '22 20:09

Paul Rowland