2016-07-21 72 views
0

后,我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 
+0

你确定这是*一个*多行字符串吗? 'Get-ChildItem'(别名'gci')不会产生像这样格式化的输出 - 你能准确告诉我们你运行的是什么命令吗? –

+0

我编辑了这篇文章:) – Atermon

+0

分配输出到一个变量,而不是做Set-Content/Get-Content。然后使用Select-Object来获取文件名和行 –

回答

0

这应该为你

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