Steve
Steve

Reputation: 27

How to delete strings from one file if they exactly mach strings from second file?

I have two text files. A Bigfile.txt and a SmallFile.txt

BigFile.txt sample:
TEXT1
TEXT2
TEXT3
TEXT4

SmallFile.txt sample:
TEXT2
TEXT3

What is the most efficient way how to delete all text strings from BigFile that are also included in SmallFile.txt?

BigFile.txt output
TEXT1
TEXT4

Upvotes: 0

Views: 38

Answers (2)

xXhRQ8sD2L7Z
xXhRQ8sD2L7Z

Reputation: 1716

$BigFile = Get-Content -Path BigFile.txt
$BigFile | ? { $_ -notin (Get-Content -Path SmallFile.txt) }

When you've tested and confirmed results are ok:

$BigFile = Get-Content -Path BigFile.txt
$BigFile | ? { $_ -notin (Get-Content -Path SmallFile.txt) } | Out-File -FilePath D:\BigFile.txt

Upvotes: 1

Anthony Stringer
Anthony Stringer

Reputation: 2001

could probably use some error checking, but you might try something like this

$bigfile = Get-Content C:\temp\BigFile.txt
$smallfile = Get-Content C:\temp\SmallFile.txt
$difference = Compare-Object $bigfile $smallfile | % {$_.inputobject}
$difference | Set-Content c:\temp\NewBigFile.txt

Upvotes: 1

Related Questions