0

I have a CSV file that I need to remove rows, here is my example

"Name","Local_IP","Remote_IP"
"Sep  1 03:55:57 pc-00017 SymantecServer mserver-4: mmt-5","172.16.48.158","22.22.22.22"
"Sep  1 03:55:57 pc-00017 SymantecServer mserver-4: mmt-5","172.16.48.158","22.22.22.22"
"Sep  1 03:55:57 pc-00017 last message repeated 3 times","","
"Sep  1 03:55:57 pc-00017 SymantecServer mserver-4: mmt-5","172.16.48.158","22.22.22.22"
"Sep  1 03:55:57 pc-00017 last message repeated 2 times","","
"Sep  1 03:55:57 pc-00017 SymantecServer mserver-4: mmt-5","172.16.48.158","22.22.22.22"

I need to remove the row that has the "last message repeated" removed.

I have tried with no luck.

$test = import-csv myfile.csv | where {$_.Name -notcontains "repeated"}
$test | export-csv myfile.csv -notype

Any help plese?

2 Answers 2

2

Something like this should work I think :

Get-Content C:\Path\myfile.csv | Where{$_ -notmatch "repeated"} | Out-File C:\Path\myNewFile.csv
Sign up to request clarification or add additional context in comments.

2 Comments

Almost... If you use Get-Content you then do not have a Name property, so it becomes Where{$_ -notmatch "repeated"}
The problem I am having now is the Get-Content strips away the CSV format and makes the data unusable.
-1

-contains is designed to work on arrays, not so good for strings. As per Sylvain's answer Where-Object {$_.Name -notmatch "repeated"} should do what you want.

For more information, check out the Technet page. If nothing else it tell you that match uses regular expressions so as not to trip over this down the line.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.