【问题标题】:Windows batch script - place strict limits on results of findstrWindows 批处理脚本 - 严格限制 findstr 的结果
【发布时间】:2013-02-06 17:18:33
【问题描述】:

我正在尝试编写一个使用 FINDSTR 在文本文件中查找关键字的脚本。给定以下命令:

findstr /s "item1" source.txt >> output.txt

我如何确保结果只提取那些包含“item1”而不是例如“item 14”的行?

我试过 /l 和 /C: 无济于事。

【问题讨论】:

    标签: batch-file


    【解决方案1】:

    您可以使用正则表达式单词边界锚。

    findstr /sr "\<item1\>" source.txt >> output.txt
    

    FINDSTR 将检测正则表达式元字符并自动将搜索字符串视为正则表达式。但最好使用/R 选项明确指定正则表达式搜索。

    单词边界锚点的 FINDSTR 实现非常不标准,但我认为它对你有用。请参阅What are the undocumented features and limitations of the Windows FINDSTR command? 了解更多信息。答案的相关部分在标题为“正则表达式单词边界”的部分的末尾附近。

    【讨论】:

    • 出色的答案!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 2023-04-02
    • 1970-01-01
    • 2010-11-02
    相关资源
    最近更新 更多