Solved

scirpt to compare Text file A with Text file B

Posted on 2012-03-20
6
317 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:
bchallis 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mapBully challenge 6 92
Vbscript 8 67
Excel .....ActiveWorkbook.SaveAs 12 33
What language/protocol is the Angular Chat? 2 16
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now