【问题标题】:Grep for windows用于 Windows 的 Grep
【发布时间】:2018-08-29 09:19:35
【问题描述】:

Old.txt 包含

apple
orange
banana

New.txt 包含

apple
orange
banana
grape
lemon

我可以使用grep 命令访问添加到New.txt 的新内容。

grep -Fxvf Old.txt New.txt > difference.txt 

现在,difference.txt 包含

grape
lemon

在Windows中,我试过了

findstr /rvc:Old.txt New.txt > difference.txt 

找到不同之处,但它也附加了Old.txt 的内容。如何在 Windows 中编写等效命令?

【问题讨论】:

    标签: cmd grep findstr


    【解决方案1】:

    您可以将DOS findstr 与以下标志一起使用:-

    /v   : Prints only lines that do not contain a match.
    /g: file   : Gets search strings from the specified file.
    

    命令类似于:-

    C:\Users\dude\Desktop>findstr /v /g:Old.txt New.txt >difference.txt
    

    现在使用type 命令检查文件输出;相当于Linux中的cat,我们看到:-

    C:\Users\dude\Desktop>type difference.txt
    grape
    lemon
    

    【讨论】:

    • Inian 谢谢它的工作。我遇到了一个新问题。 Old.txt 为空且 New.txt 包含一些内容时不起作用
    • @MusieMeressa:/g: 标志后面的文件必须有一些条目,正如您在上面的示例中看到的那样,它从 'Old.txt' 中查看每一行并在其他文件。因此,只有非空文件必须在 /g: 标志下才能使此解决方案起作用。那么,您可以试试findstr /v /g:New.txt old.txt,现在New.txt 确实有条目,这将起作用!
    • 即使我更改顺序,当其中一个文件为空时它也不起作用。无论如何谢谢。
    【解决方案2】:

    除非您被限制在您的 PC 上安装任何东西,否则请考虑安装类似 *nix 的工具(例如 GnuWin32)的端口并继续使用 grep。

    【讨论】:

    • 你对,我不能在 PC 上安装任何东西
    • 或者使用最近添加到 Windows 10 的 Linux shell(假设是操作系统)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-10
    • 1970-01-01
    • 2012-04-26
    • 1970-01-01
    相关资源
    最近更新 更多