1

This other question is excellent for joining two files. I need to do sort of the opposite. I need to remove lines from File A that are in File B.

This question is similar to this other question, except that question is for unix and this is for the windows command shell (cmd.exe).

I would like to use tools that are native to Windows 7 Pro.

Update:

The files are hosts files. Each one has lines consisting of:

  • 127.0.0.1 host.domain.com

or

  • 0.0.0.0 host.domain.com

or

  • # this is a comment

Files may have up to 200,000 lines. Spaces and tabs may be present.

Although I prefer it to be preserved, order does not affect function.

Here are some examples of hosts files:

(Don't worry about 0.0.0.0 vs '127.0.0.1` for this question.)

Community
  • 1
  • 1

3 Answers3

3
findstr /v /x /g:"fileb" "filea">resultfile

find strings in file A that do not (/v) exactly match (/x) those in fileb.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    You really should add the `/L` switch to force a literal search, as FINDSTR will interpret the strings as regular expressions if the first line in the file contains at least one meta character, and the entire line is a valid regex. With that change, this *should* work. But there is a [nasty FINDSTR bug when using multiple literal search strings](https://stackoverflow.com/q/8921253/1012053) such that it may miss some matches. – dbenham Jun 04 '17 at 04:33
  • @dbenham Thank you for your comment. Wouldn't this count as a single literal search string and bypass that bug? – RockPaperLz- Mask it or Casket Jun 04 '17 at 04:47
  • @RockPaperLizard - No, each line within /G:file counts as a separate search string. So the nasty bug is in play. Follow the link in my prior comment for a full description of the bug. – dbenham Jun 04 '17 at 12:42
  • @dbenham Given that bug, I wonder if there is another solution. – RockPaperLz- Mask it or Casket Jun 04 '17 at 17:14
0

You did not provided a single specification for this problem! May the files have special characters or spaces? How many lines they may have? Must the output file be in the same order of FileA?

This solution may or may not work depending on a series of factors...

@echo off
setlocal

rem Load lines from File A into "fileA" array
for /F "delims=" %%a in (FileA.txt) do set "fileA["%%a"]=1"

rem Remove elements from "fileA" array that are in File B
for /F "delims=" %%b in (FileB.txt) do set "fileA["%%b"]="

rem Show remaining elements
for /F "tokens=2 delims=[]" %%a in ('set fileA[') do echo(%%~a
Aacini
  • 65,180
  • 12
  • 72
  • 108
0

As dbenham pointed out in a comment, there is a bug of findstr with multiple literal search strings -- reference this thread: Why doesn't this FINDSTR example with multiple literal search strings find a match?. To work around this, you could do the following in command prompt:

copy "fileB" "fileC" & (for /F usebackq^ delims^= eol^= %L in ("fileA") do findstr /V /X /C:"%L" "fileC" > "fileC") & move /Y "fileC" "fileB"

Or in a batch file:

copy "fileB" "fileC"
for /F usebackq^ delims^= eol^= %%L in ("fileA") do (
    findstr /V /X /C:"%%L" "fileC" > "fileC"
)
move /Y "fileC" "fileB"

This first copies fileB as fileC, then removes lines from fileC matching those of fileA one after another, then replacing the original fileB by the modified fileC.

aschipfl
  • 33,626
  • 12
  • 54
  • 99