Ben
Ben

Reputation: 83

Issues replacing new line in PowerShell script

I have a CSV file which has the following for example:

data1,data2,data3,data4,data5
,,,,,
data6,data7,data8,data9,data0

New lines are CRLF (`r`n) as viewed in Notepad++.

I have this in my PowerShell script to remove the line with all commas:

(Get-Content $csvPath) | Foreach-Object {$_ -replace ",,,,,`r`n", ""} | Set-Content $csvPath2

The script will not detect the returns. Even if I remove the commas and just have `r`n. However, I can replace the commas with `r`n without issues.

How can I remove that entire line of commas so that it appears as below?

data1,data2,data3,data4,data5
data6,data7,data8,data9,data0

Upvotes: 1

Views: 663

Answers (1)

Knows Not Much
Knows Not Much

Reputation: 31546

Use:

$lines = get-content C:\temp\test.txt | ?{$_ -notlike ",,,,*"}
$lines.count

Here the file test.txt contains:

data1,data2,data3,data4,data5
,,,,
data6,data7,data8,data9,data0

The output is:

data1,data2,data3,data4,data5
data6,data7,data8,data9,data0

Upvotes: 3

Related Questions