Solved

scirpt to compare Text file A with Text file B

Posted on 2012-03-20
6
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 56

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
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…
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

628 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