简体   繁体   English

如何在多个文件中搜索字符串并在 Excel 或 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

I have found an answer that gives the list of files in powershell here我找到了一个答案,在此处提供了 powershell 中的文件列表

How to search a string in multiple files and return the names of files in Powershell? 如何在多个文件中搜索字符串并在Powershell中返回文件名?

but I need to know the line number and line text also where that text is found.但我需要知道行号和行文本以及找到该文本的位置。 After this I need to write the output to an excel or csv.在此之后,我需要将输出写入 excel 或 csv。

edit: Get-ChildItem -recurse |编辑:Get-ChildItem -recurse | Select-String -pattern "ftp" | Select-String -pattern "ftp" | Write-Host script is giving me the output as C:\\codes\\prog1.txt:10:FTP but I want the output as path - C:\\codes\\prog1.txt line number-10 Line text-FTP "server1" in Excel写主机脚本给我的输出为 C:\\codes\\prog1.txt:10:FTP 但我希望输出为路径 - C:\\codes\\prog1.txt line number-10 Line text-FTP "server1" in电子表格

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM