Solved

scirpt to compare Text file A with Text file B

Posted on 2012-03-20
6
349 Views
Last Modified: 2012-03-27
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
0
Comment
Question by:fireburn11
6 Comments
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37744710
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
0
 
LVL 16

Expert Comment

by:Dale Harris
ID: 37744731
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}
}
}
0
 
LVL 8

Accepted Solution

by:
Brent Challis earned 500 total points
ID: 37745367
A more efficient way to implement the check is to test with -contains:

$fileA = get-content "C:\data\Development\Powershell\ScriptQuestion\TextFileA.txt"
$fileB = get-content "C:\data\Development\Powershell\ScriptQuestion\TextFileB.txt"

foreach ($lineA in $fileA)
{
    if ($fileB -contains $lineA)
    {
        Write-Host $lineA
    }
}

When I tested this on two text files of 1000 lines each with 434 matches the -contains version ran approximately 2 1/2 times faster.

If you wanted the output to go to a file rather than the console you could replace

Write-Host $lineA

with
$lineA | Out-File fileName -Append
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Dale Harris
ID: 37745373
Oh, very nice improvement bchallis.  Thanks for testing that.  I knew it wasn't the most efficient either with two foreach loops nested.
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 37750561
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
0
 

Author Comment

by:fireburn11
ID: 37771531
Great solution!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This video teaches viewers about errors in exception handling.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question