3

Let's say I have:

text.txt with

1
2
3
4
5

and another text2.txt with

9
4
2
1
7

And I want to extract the duplicates:

2
1
4

Note: I'm using windows, I want it to be as easy as possible

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
RaRe MoDz
  • 31
  • 1
  • 3

2 Answers2

3

I want to extract the duplicates

Use the following command line:

Findstr /i /x /g:text.txt text1.txt

Where:

  • /I Case-insensitive search
  • /X Prints lines that match exactly.
  • /G:StringsFile Get search string from a file

Source: Findstr - Search for strings - Windows CMD - SS64.com

Example:

F:\test>type text.txt
1
2
3
4
5
F:\test>type text1.txt
9
4
2
1
7
F:\test>Findstr /ixg:text.txt text1.txt
4
2
1
F:\test>Findstr /ixg:text1.txt text.txt
1
2
4

Note that there is no easy way to get the output in the order specified in the question:

2
1
4

as neither of the files contain the lines in that order.


Further Reading

DavidPostill
  • 153,128
  • 77
  • 353
  • 394
0

With a Unix environment (cygwin on Windows), I would first sort both files (sort text.txt > text.txt.sorted, idem for the second file), then use comm -12 text.txt.sorted text2.txt.sorted

Frédéric Loyer
  • 1,080
  • 3
  • 7
  • Ok, some hints for Windows (get `comm` from Cygwin) – Frédéric Loyer Nov 19 '21 at 23:30
  • 2
    If you are using a Linux tools, there are easier ways of doing this - for example **sort text.txt text2.txt | uniq -d** will concat the 2 files in memory, order them and list any duplicates. I expect there are better ways of doing it under Windows. – davidgo Nov 20 '21 at 06:08
  • I don't have a Windows box, but one way to do this with just Windows tools would be to concat the files together, then look for dupes with the following commands - copy text.txt text2.txt > both.txt and then Get-Content both.txt | Group-Object | Where-Object { $_.Count -gt 1 } | Select -ExpandProperty Name (As per https://stackoverflow.com/questions/43808886/windows-powershell-to-find-duplicate-lines-in-a-file ) – davidgo Nov 20 '21 at 06:29
  • Windows have `sort` and `fc` to compare files. But on the command line it's better to use powershell which is far more powerful – phuclv Nov 20 '21 at 07:51