如何在多个文件中搜索字符串并使用Excel中的行号/文本或Powershell中的csv返回文件名

时间:2015-09-30 07:42:35

标签: string powershell search line

我找到了一个答案,在这里给出了PowerShell中的文件列表

How to search a string in multiple files and return the names of files in Powershell?

但我需要知道找到该文本的行号和行文本。在此之后,我需要将输出写入excel或csv。

编辑: Get-ChildItem -recurse | Select-String -pattern" ftp" | Write-Host脚本给我的输出为C:\ codes \ prog1.txt:10:FTP但我希望输出为路径 - C:\ codes \ prog1.txt行号-10行文本-FTP" server1的"在Excel中

1 个答案:

答案 0 :(得分:2)

试试这个(不知道你是否只想要文件名或文件的路径,只需删除你不想要的那个):

Get-ChildItem -recurse | Select-String -pattern "string" | Select-Object path,line,linenumber,filename | Export-Csv -Path c:\somepath\result.csv