过滤CSV回车
试图找出如何过滤特定CSV项目。过滤CSV回车
CSV内容:
ComputerName,Results
computerA ," KB00000 is not installed
c:\SomeTestFile Version is 2
c:\SomeTestFile Version is 11.0"
ComputerB ,%windir%\somefile.exe is Version 1.2
我需要它在第二列基于所述回车第一过滤并随后筛选不同的参数,如“是version10”或“KB00000丢失”了。
在CSV拟输出:
ComputerName,Results
computerA ,c:\SomeTestFile Version is 2
computerA ,c:\SomeTestFile Version is 11.0
ComputerB ,%windir%\somefile.exe is Version 1.2
有一个编码块。我可以导入文件,但坚持过滤。
$CSV = Import-Csv File.csv
与您指定csv
这个脚本:
## Q:\Test\2017\08\24\SO_45870618.ps1
$FileIn = '.\FileIn.Csv'
$FileOut = '.\FileOut.Csv'
$Csv = Import-Csv $FileIn
$Csv
$CsvNew = ForEach ($Row in $Csv) {
If (!($Row.ComputerName)){
If ($Last) {$Row.ComputerName = $Last}
}
$Last = $Row.ComputerName
ForEach ($line in ($Row.Results -Split('\r?\n'))){
If ($line -NotMatch '^\s*KB\d+\s+is'){
[PSCustomObject]@{ComputerName = $Last.Trim()
Results = $line}
}
}
}
"------------"
$CsvNew
$CsvNew | Export-Csv $FileOut -NoTypeInformation
都会有这样的输出:
> Q:\Test\2017\08\24\SO_45870618.ps1
ComputerName Results
------------ -------
computerA KB00000 is not installed ...
ComputerB %windir%\somefile.exe is Version 1.2
------------
computerA c:\SomeTestFile Version is 2
computerA c:\SomeTestFile Version is 11.0
ComputerB %windir%\somefile.exe is Version 1.2
所得FileOut.csv:
> gc .\FileOut.Csv
"ComputerName","Results"
"computerA","c:\SomeTestFile Version is 2"
"computerA","c:\SomeTestFile Version is 11.0"
"ComputerB","%windir%\somefile.exe is Version 1.2"
不幸的是我没有得到相同的输出。它并没有删除ComputerA的结果中的回车。 GC C:\ TEMP \ FileOut.csv “计算机名”, “结果” “computerA”, “KB00000未安装 C:\ SomeTestFile版是2 C:\ SomeTestFile版是11.0" ” ComputerB ”, “%WINDIR%\ somefile.exe是1.2版本” – user2542412
你** **没注意到我的'提供您的CSV看起来like'?请[编辑](https://stackoverflow.com/posts/45870618/edit)您的问题,以包含您的真正csv。不要置评论中。 – LotPings
有问题的更新CSV并直接从记事本发布。谢谢 – user2542412
您的示例内容是CSV的实际原始内容(在文本编辑器中打开时)? –
我键入它在这里,但如果你把它放在CSV它会是什么样子: 计算机名,结果 computerA,” KB00000没有安装 C:\ SomeTestFile版本是2 C:\ SomeTestFile版本是11.0" ComputerB,%WINDIR%\ somefile.exe是1.2版 – user2542412
请[编辑]你的问题和复制/粘贴文本。 –