通过多次搜索搜索文件并删除文件中的匹配项

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

我有一个包含单列 IMEI (file1.txt) 的文本文件。我有第二个文件,其中包含一列 IMEI (file2.txt)。

我想做的是获取 file2.txt 中的每个 IMEI 并搜索 file1.txt 找到任何匹配项,然后在找到匹配项时删除 file1.txt 中的那一行.

困难的是我需要一次执行多个搜索。由于每个文件中有数百个 IMEI,因此一次执行一个将花费相当长的时间。

有人对完成这个任务有什么建议吗?

脚本:

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

上面的方法有效,但因为我正在搜索几百个 "matches",它每次都 returns "not matches",所以它返回了数千个结果。

你可以在这里使用compare-object

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

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

这将删除相同的内容,只返回第一个文件中的内容,删除所有类似 IMEI 的内容。然后您可以使用 Out-File 导出它并覆盖您之前的文件。