2017-08-03 42 views
0

我想操纵输出到适当的列,以便以后可以在Excel中打开文件。基本上通过复制输出并将其粘贴到具有分隔符选项(导入向导)的Excel中。操纵选择字符串对象的输出

的SourceFile:A.TXT
https://rent.com

代码

[email protected]("rent.com") 
$files=Get-ChildItem "D:\test\*.*" -Recurse 
$files |Select-String -Pattern $pat |Out-File "D:\output\result.txt" 

但输出我得到的是:

D:\test\a.txt:1:https://rent.com 

如何操作的输出,使我得到:

D:\test\a.txt~1~https://rent.com 

然后我可以使用〜作为分隔符并导入到Excel中。

感谢您的帮助!

回答

0

如何:

[email protected]("rent.com") 
$files=Get-ChildItem "D:\test*.*" -Recurse 
$files | 
    Select-String -Pattern $pat | 
    %{ "{0}~{1}~{2}" -f $_.Path, $_.LineNumber, $_.Line } | 
    Out-File "D:\output\result.txt" 

Try it online

参考文献:

Select-String Outputs

+0

感谢道格,但问题是我可以在文本文件本身中有':'。 以上命令会导致 D〜\ test \ a.txt〜1〜https〜// rent.com – HungryProgrammer

+0

我相应地更新了我的答案...... –

+0

Hi Doug,对于$ _。Line列,有没有我可以得到匹配的价值而不是整条线;所以输出将是D:\ test \ a.txt:1:rent.com,试图查看匹配参数。 – HungryProgrammer

0

其他方法:

[email protected]("tec") 
$files=Get-ChildItem "D:\test*.*" -Recurse -file 
$files | Select-String -Pattern $pat | %{$_.ToString().Replace(':', '~')} | Out-File "D:\output\result.txt" 
+0

我起初有同样的解决方案,但它不起作用,因为文件名和结果都有一个':'不应该用'〜'代替。 –