Solved

scirpt to compare Text file A with Text file B

Posted on 2012-03-20
6
329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

920 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