Amazon Dies In Darkness
Amazon Dies In Darkness

Reputation: 5823

How to remove lines from File A that are in File B in Windows command shell?

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:

or

or

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.)

Upvotes: 1

Views: 253

Answers (3)

aschipfl
aschipfl

Reputation: 34899

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.

Upvotes: 0

Aacini
Aacini

Reputation: 67206

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

Upvotes: 0

Magoo
Magoo

Reputation: 79982

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

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

Upvotes: 3

Related Questions