soulbleach has asked for the wisdom of the Perl Monks concerning the following question:
I have two files file1.txt and file2.txt each file contains lines with certain strings that I need to extract and pass into array's (at least thats how I think it should be done). I then want to compare the contents of the two arrays and remove all entries that have the same string in both so I end up with a list that contains only the unique entries/ lines from file2.txt.
So far I have two seperate but fairly similar scripts that will perform part of each job. I'm completely new to Perl and have very limited knowledge of scripting in general, small amount of Lua, PHP and a bit more knowledge of bash: please be gentle.
I would like to know how to join these two together so they do both parts of the job in one script. I've tried a number of options where I nest loops within each other but I find that it will do one part then the next and then loop back to the beginning and miss out the subsequent next steps e.g. I'll get RC0000 - RC9999 printed out but it won't then go and pull the lines from file 2.
This most likely sounds nonsensical so anything I can do to make it clearer please ask.
Thanks very much in advance.
script1 which acts on file1 and outputs an array containing entries in the format RC0000:
use strict; use warnings; my $filename = "file1.txt"; my $filename2 = "file2.txt"; my $logger = "log.txt"; my %seen; my @unique; my @strings; my %seen2; my @unique2; my @strings2; my $handle2; my $s; my $s2; my $seen; my $seen2; my $line2; open my $handle, '<', $filename || die "$0: Can't open $filename for r +eading: $!"; open my $FH, '>', $logger || die "$0: Can't open $logger for writing: +$!"; while (my $line = <$handle>) { my @strings = $line =~ m/(RC[0-9][0-9][0-9][0-9])/; foreach my $s ( @strings ) { next if $seen{ $s }++; push @unique, $s; print $FH "@strings\n"; # push output to file log.txt for + testing } } close $FH or die $!;
script2 which works on file2 and pulls out all lines that contain an entry in the format RC=0000:
use strict; use warnings; my $filename2 = "file2.txt"; my %seen2; my @unique2; my @strings2; my @line2; open my $handle2, '<', $filename2 || die "$0: Can't open $filename2 fo +r reading: $!"; while (my $line2 = <$handle2>) { my @strings2 = $line2 =~ m/(RC=[0-9][0-9][0-9][0-9])/; foreach my $s2 (@strings2) { foreach my $s2 ( @strings2 ) { next if $seen2{ $s2 }++; push @unique2, $s2; print "$line2\n"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie, need help extracting strings from two files and comparing
by Athanasius (Archbishop) on Dec 03, 2014 at 17:10 UTC | |
|
Re: Newbie, need help extracting strings from two files and comparing
by GotToBTru (Prior) on Dec 03, 2014 at 16:56 UTC | |
|
Re: Newbie, need help extracting strings from two files and comparing
by stevieb (Canon) on Dec 03, 2014 at 17:34 UTC | |
by soulbleach (Initiate) on Dec 09, 2014 at 12:23 UTC | |
|
Re: Newbie, need help extracting strings from two files and comparing
by karlgoethebier (Abbot) on Dec 03, 2014 at 18:36 UTC |