Reputation: 183
I have a well-formed CSV file that I want to read, reformatting some values while doing so. For some reason the code below does not work ($csv
variable ends up being empty):
$csv = Import-Csv "result.log" -Delimiter "|" | ForEach-Object {
$_.PSObject.Properties.Value | ForEach-Object {
if ($_ -like '*---*') { $_ = "" }
}
}
What am I doing wrong?
Upvotes: 2
Views: 262
Reputation: 23355
Your result is blank because you aren't returning anything to the pipeline (you are just changing the value of one or more properties but then not outputting them).
There might be a simpler solution but I think this achieves what you want:
$CSV = Import-Csv "result.log" -Delimiter "|" | Foreach-Object {
$Output = New-Object -TypeName PSObject
$_.PSObject.Properties | Foreach-Object {
If ($_.Value -like '*---*') { $_.Value = '' }
$Output | Add-Member -Name $_.Name -Value $_.Value -MemberType NoteProperty
}
$Output
}
This performs the following for each row of the CSV:
Add-Member
to add those properties to our objectThe result of this goes to the $CSV
variable which you then could output as CSV via Export-CSV
(or you could skip putting it in the variable and use Export-CSV
on the end of the outer ForEach-Object
).
Upvotes: 1