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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grep contents of fileA in fileB 1

Status
Not open for further replies.

sasuser2006

Technical User
May 8, 2006
32
US
Guys, sorry if this is a dumb question but I'm looking to write a Perl script that will search the contents of fileA to see if the contents of fileB are in there. If they are in there write records out that match to file and the records that aren't to another file.

fileA is fixed and I'm only wanting to check the first 16 bytes. fileB will only be the 16 byte matching key.

Any help is much appreciated. Thanks in advance.
 
then get the first 16 bytes of fileA into a scalar variable and see if you find a match for it in fileB. Posting sample data would help answer the question.
 
I have a script somewhere that can help. I'll find it a post the code.

However, if I may make a suggestion. This could be accomplished much faster and easier through a database. Download mysql and install it. Create two single field tables in a database and query them against one another. Trust me, once you get the hang of it, you'll never look back to using flat files.

Just my $.02.

Mark
 
I'm even less knowledgeable about mysql.

Can you tell me how I would get the first 16 bytes into a scalar and then match it?

fileA contents are:

12345ABC90123456ABC
234519YJF3094239DEF
10329480RST83920GHI

fileB contents are:

12345ABC90123456
123908234BCD3402
4320FGR028430920

I want to know if the 16 byte alpha-numeric field in fileB exists in fileA. Thanks for the advice already and any future advice given.
 
Here is what I'm working on...but it doesn't seem to work...doesn't logically spit out the right records...i.e...the records in matchfile.txt aren't getting exported to match_record.txt...it just seems a random record is matching for some reason...

command run: perl grep.pl < file_to_match.txt

contents of grep.pl:

#!/usr/bin/perl -w

use strict;

my $reccnt=0;

open(fh_matchfile,"<fileL.txt") or die "Error opening the match file!\n";
open(fh_matchrecord,">match_record.txt") or die "Error opening the badrecord file!\n";
open(fh_nomatchrecord,">no_match_record.txt") or die "Error opening the goodrecord file!\n";

my @matchlist = (<fh_matchfile>);

while(<STDIN>) {
chomp;
$reccnt++;
if ($reccnt eq @matchlist){
print fh_matchrecord "$_\n";
}
else {
print fh_nomatchrecord "$_\n";
}
}

 
sorry....fh_matchfile,"<fileL.txt"...should read...fh_matchfile,"<matchfile.txt"...thanks.
 
are you checking every line of fileB against every line of fileA? Find all matches for every 16 byte key in fileA or find only the first match? Or what?

Maybe this is what you want:

Code:
use strict;
use warnings;

open(FA,'<filea.txt') or die "$!";
my %FILEA = ();
while(<FA>){
   $FILEA{substr($_,0,16)}++;
}   
close(FA);
open(FB,'<fileb.txt') or die "$!";
open (RESULTS,'>results.txt') or die "$!";
while(<FB>){
   chomp;
   if (exists $FILEA{$_}) { 
      print "Key: $_ found$/"; 
      print RESULTS "Key: $_ found$/";
   }
}
close(FB);
close(RESULTS);
 
I want to check every line of fileB against every line of fileA. Thanks...I'll try this...
 
ahh, we were posting at the same time, look at my code and see if you can adapt it to your needs.
 
This was exactly what I was needing!!! I added an else statement to get the no matches but it worked perfectly. Thank you so much for your help. One last question...if I want to output all of the record not just the substringed portion what would I do?

Thanks again!!!

#!/usr/bin/perl -w

use strict;
use warnings;

open(FA,'<CB.txt') or die "$!";
my %FILEA = ();
while(<FA>){
$FILEA{substr($_,0,16)}++;
}
close(FA);
open(FB,'<IB.txt') or die "$!";
open (RESULTS,'>matches.txt') or die "$!";
open (NOMATCHES,'>no_matches.txt') or die "$!";

while(<FB>){
chomp;
if (exists $FILEA{$_}) {
print "Key: $_ found$/";
print RESULTS "$_ $/";
}
else {
print "Key: $_ not found$/";
print NOMATCHES "$_$/";
}
}
close(FB);
close(RESULTS);
close(NOMATCHES);

 
If I understand you correctly,

change this:

Code:
while(<FA>){
   $FILEA{substr($_,0,16)}++;
}

to:

Code:
while(<FA>){
   chomp;
   $FILEA{substr($_,0,16)}=$_;
}


and change this:

Code:
while(<FB>){
   chomp;
   if (exists $FILEA{$_}) {
      print "Key: $_ found$/";
      print RESULTS "$_ $/";
   }
   else {
         print "Key: $_ not found$/";
         print NOMATCHES "$_$/";
   }
}



to:
Code:
while(<FB>){
   chomp;
   if (exists $FILEA{$_}) {
      print "Key: $FILEA{$_} found$/";
      print RESULTS "$FILEA{$_}$/";
   }
   else {
         print "Key: $_ not found$/";
         print NOMATCHES "$_$/";
   }
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top