【问题标题】:How do I find a string in text files and print the matches in a Windows batch file?如何在文本文件中查找字符串并在 Windows 批处理文件中打印匹配项?
【发布时间】:2013-06-18 09:00:13
【问题描述】:

有没有一种方法可以在数千个文本文件中搜索已定义的字符串并打印匹配的文件的名称?

【问题讨论】:

  • 这不是批处理,但你可以试试记事本++中的“在文件中查找”菜单,效果很好

标签: batch-file batch-processing


【解决方案1】:
findstr /M "searched string" *.txt > matchingFiles.out

来自findstr /? 文档:

/M         Prints only the filename if a file contains a match.

【讨论】:

    【解决方案2】:

    不确定要回答哪个操作系统平台,

    但 Linux/Unix 系统可以有以下命令用于此目的。

    find . -name '*.ext' -type f | xargs fgrep 'pattern to be searched'
    

    这将从当前目录开始搜索文件并检查“要搜索的模式”。带通配符的文件名模式可以应用在我使用过'*.ext'的地方

    【讨论】:

    • 哦,对不起,我真的忘了提及我的操作系统。它是 Windows 7 :/ 但还是谢谢你的回答 :)
    • 好的。链接stackoverflow.com/questions/14440692/… 中提到了一个稍微相反的 powershell 选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多