Link to home
Start Free TrialLog in
Avatar of fireburn11
fireburn11

asked on

scirpt to compare Text file A with Text file B

Hi Guys,

I need a script that can compare text file A with text file B. If any line in text file A is found in text file B, print that line out onto the screen or into a text file.

it can be vb, powershell, batch.

Please advise.

Thanks
Avatar of Dale Harris
Dale Harris
Flag of United States of America image

I think there's a really easy command for compare-object for text files.

http://technet.microsoft.com/en-us/library/ee156812.aspx

Compare-Object $(Get-Content c:\scripts\x.txt) $(Get-Content c:\scripts\y.txt)

DH
Well after looking at what you're doing, I think you're making a list in one file and the output is in the other.

In this case, you could do a simple foreach loop in Powershell to do it.

$matches = get-content "C:\Scripts\FileA.txt"

$output = get-content "C:\Scripts\FileB.txt"

foreach ($match in $matches){
foreach ($line in $output){
if ($match -eq $line){Write-Host $line}
}
}
ASKER CERTIFIED SOLUTION
Avatar of Brent Challis
Brent Challis
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, very nice improvement bchallis.  Thanks for testing that.  I knew it wasn't the most efficient either with two foreach loops nested.
Avatar of Bill Prew
Bill Prew

Here's how to do it with a simple BAT or command line command:
findstr /i /g:a.txt b.txt

Open in new window

~bp
Avatar of fireburn11

ASKER

Great solution!