替换多行中的字符串

时间:2016-07-21 09:54:57

标签: powershell replace

在我获得gci后,我得到了以下输出:

C:\mine\this is the filename I want to keep -blah blah.csv:11:"Windows User","BrftD","Bledaf","Internal user"
C:\mine\it is a different filename I want to keep -bleh blih.csv:12:"Windows User","BrftD","Bledaf","Internal user"

我需要一个替换来替换“\”之前和“ - ”之后的任何内容,直到“:”加上“”和“”,所以输出将是

"this is the filename I want to keep","Windows User","BrftD","Bledaf","Internal user"
"it is a different filename I want to keep","Windows User","BrftD","Bledaf","Internal user"

脚本是:

gci *.csv | Select-String -pattern '"Windows User"'|Set-Content csvdatacol.csv
Get-Content csvdatacol.csv

1 个答案:

答案 0 :(得分:0)

这应该对你有用

Get-ChildItem *.csv |
  Select-String -Pattern '"Windows User"' | 
  % { '"{0}",{1}' -f ($_.FileName -replace '-.*', '').TrimEnd(), $_.Line }