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"; } } }

In reply to Newbie, need help extracting strings from two files and comparing by soulbleach

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.