Search through a file with multiple searches and remove matches in the file

41 Views Asked by At

I have a text file containing a single column of IMEIs (file1.txt). I have a second file containing a single column of IMEIs (file2.txt).

What I'm trying to do is take each IMEI in file2.txt and search through file1.txt locating any matches and then when it finds a match, remove that line in file1.txt.

What's making it difficult is the multiple searches I need to perform at once. Doing each one at a time would take quite a while as there are hundreds of IMEIs in each file.

Does anyone have any suggestions to accomplish this task?

Script:

$orders = Get-Content c:\file2.txt
foreach ($order in $orders) {
    Get-Content file1.txt | Select-String -Pattern "$order" -NotMatch
}

The above works but because I'm searching a couple hundred "matches", it returns the "not matches" every time so it's returning thousands of results.

1

There are 1 best solutions below

1
On BEST ANSWER

You can use a compare-object here.

$file1 = (1234,5678,9123)
$file2 = (1234)

$remove = Compare-Object $file1 $file2
$remove = $remove | Where{$_.SideIndicator -ne '=>'}
$file1 = $remove.inputobject

That will remove what is equal and only give back what is in the first file, removing all like IMEI's. You can then just export it with Out-File and overwrite your previous file.