AK26
AK26

Reputation: 31

Removing lines from a CSV

I have a csv document with multiple headers like:

"Date","RQ","PM","SME","Activity","Status code"
"2/2/12","6886","D_WV","John Smith","Recent","2004"

and a text document that is just a list of status codes, one per line.

I am trying to figure out how to remove all lines from the CSV that contain the status codes from the text file.

So far I have tried using:

$m = gc textfile.txt
Select-String data.csv -Pattern $m -NotMatch

However that leaves me with extra data such as

data.csv:1"Date","RQ","PM","SME","Activity","Status code"
data.csv:2"2/2/12","6886","D_WV","John Smith","Recent","2004"

I have also tried:

gc data.csv | ? { $_ -notlike $m }

That uses the proper formatting but does not want to remove any of the values. Any help is much appreciated.

Upvotes: 2

Views: 4933

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200193

I'd suggest a different approach to avoid false positives:

$m = Get-Content textfile.txt
Import-Csv data.csv `
  | ? { $m -notcontains $_."Status code" } `
  | Export-Csv output.csv -NoTypeInformation

Upvotes: 1

mjolinor
mjolinor

Reputation: 68243

Those matchinfo objects from select-string can be confusing. Does this do what you need?

$m = gc textfile.txt
select-string data.csv -pattern $m -notmatch |
select -expand line

Upvotes: 1

Related Questions