Grep for windows

2020-04-06 03:03发布

问题:

Old.txt contains

apple
orange
banana

And New.txt contains

apple
orange
banana
grape
lemon

I can access new contents which are added to New.txt using grep command.

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

Now, difference.txt contains

grape
lemon

In Windows, I have tried

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

to find the difference but it append contents of Old.txt too. How can I write equivalent command in Windows?

回答1:

You can use DOS findstr with the following flags:-

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

The command would be something like:-

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

Now checking the file output using the type command; equivalent to cat in Linux, we see :-

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


回答2:

Unless you are restricted from installing anything on your PC, considering installing ports of *nix-like tools such as GnuWin32 and continue to use grep.



标签: cmd grep findstr