如何在多个文件和 return 文件名中搜索 Excel 中的行 number/text 或 Powershell 中的 csv

How to search a string in multiple files and return file name with line number/text in an Excel or csv in Powershell

我找到了一个答案,在此处给出了 powershell 中的文件列表

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

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

编辑: 获取 ChildItem -递归 | Select-字符串-模式"ftp" | Write-Host 脚本给我的输出为 C:\codes\prog1.txt:10:FTP 但我希望输出为路径 - C:\codes\prog1.txt line number-10 Line text -FTP "server1" 在 Excel

试试这个(不知道你是只想要文件名还是文件路径,把你不想要的去掉):

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