Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search 2 files and then compare

Status
Not open for further replies.

brandall484

Programmer
Apr 12, 2012
1
US
Hi, I am new to perl and am trying to get a project done but don't know where to start!! I am pretty frusturated!

My problem is this: I need to search a file for a text string such as S13OP*. there are hundreds of variants of that file name in the file I want to search, but all have that prefix. then I need to run that same command on a second file and then compare them. this is to double check that what I intended to have for data, I do actually have. is there a simple way to do this?

I keep getting hungup because I am not sure how to search fopr a string such as S13OP*, and I also have no idea how to create a new file with the results of the searches so that they can be compared!

Thanks for the help!!!
 
I have a small script that does almost the same job. I have altered it to just look at entries in the two files that begin with S13OP. You run it as follows:

perl listcompare.pl c:\temp\fileA.txt c:\temp\fileB.txt

You obviously need to change the two file paths to match your requirements. The code is:

Code:
#!c:\perl\bin\perl
# Initialise variables
use strict;
my ($line,@uniq,@unit1,@uniq2,@unit2,%seen,%seen2);
my $firstfile = $ARGV[0];			# read first file
my $secondfile = $ARGV[1];			# read second file
unless ( defined($secondfile) ) {
	die "the syntax should be 'perl listcompare.pl <file1> <file2>'\n";
	}
########################################
open (REPORT, ">filecomp.txt") or print "unable to create filecomp.txt: $!";
########################################
# Put heading in report file
print REPORT "###################################################################################\n";
print REPORT " Comparing entires in $firstfile with column in $secondfile\n";
print REPORT "###################################################################################\n";
########################################
open (FILE1, "$firstfile");
while (<FILE1>) {
	chomp;
	unless (/^\s*$/) {
		my $line=trim($_);
		if ($line=~m/^S13OP/) {
			$seen{$line}++;
		}
	}
}
@uniq = keys %seen;
@unit1 = sort (@uniq);

open (FILE2, "$secondfile");
while (<FILE2>) {
	chomp;
	unless (/^\s*$/) {
		my $line=trim($_);
		if ($line=~m/^S13OP/) {
			$seen2{$line}++;
		}
	}
}
@uniq2 = keys %seen2;
@unit2 = sort (@uniq2);

sub trim {
    my @out= @_;
    for (@out) {
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray ? @out : $out[0];
}
#######################################
my (%exclude,%exclude2,@justin1,@justin2,%inboth,@both,$len_justin1,$len_justin2,$len_both);
%exclude =  map {$_,$_} @unit2;
@justin1 = grep {!exists($exclude{$_})} @unit1; 
print REPORT "\nSection 1: exclusive to $firstfile:\n";
print REPORT "$_\n" for @justin1;
$len_justin1 = @justin1;
print REPORT "\n$len_justin1 entries \n";

print REPORT "\nSection 2: exclusive to $secondfile:\n";
%exclude2 =  map {$_,$_} @unit1;
@justin2 = grep {!exists($exclude2{$_})} @unit2; 
print REPORT "$_\n" for @justin2;
$len_justin2 = @justin2;
print REPORT "\n$len_justin2 entries \n";


%inboth =  map {$_,$_} @unit2;
@both = grep {exists($inboth{$_})} @unit1; 
print REPORT "\nSection 3: in both files:\n";
print REPORT "$_\n" for @both;
$len_both = @both;
print REPORT "\n$len_both entries \n";


print "\nOutput written to file_comp.txt\n";
If you don't understand any of it just ask.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top