Grep for windows
Asked Answered
H

2

6

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?

Historiographer answered 1/8, 2016 at 13:44 Comment(0)
S
6

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
Socialization answered 1/8, 2016 at 14:0 Comment(3)
Inian Thanks it works. I got a new problem. It doesn't work when Old.txt is empty and New.txt contains some contentHistoriographer
@MusieMeressa: The file after the /g: flag must have some entries, As you can see in the above example, it sees each line from 'Old.txt' and performs a invert search on the other file. So only the non-empty file must be under /g: flag for this solution to work. So, can you try findstr /v /g:New.txt old.txt, now that New.txt does have entries, this will work!Socialization
It doesn't work when one of the file is empty even if I change the order. Thanks anyhow .Historiographer
B
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.

Brahms answered 1/8, 2016 at 13:54 Comment(2)
u r right, I am not allowed to install anything on the PCHistoriographer
Or use the Linux shell that recently was added to Windows 10 (assuming that's the OS)Merganser

© 2022 - 2024 — McMap. All rights reserved.