Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

use to files - grep one to eliminate entries from another file.

by newkendall (Initiate)
on Jan 23, 2012 at 17:07 UTC ( #949464=perlquestion: print w/ replies, xml ) Need Help??
newkendall has asked for the wisdom of the Perl Monks concerning the following question:

Good morning, I'm new at all of this but managers want me to look at an Oracle alert log . I call this mail. I want to eliminate from mail.fil certain lines with numbers contained in file THIDSerrs.

!/usr/local/perl-5.12.3/bin/perl use strict; use warnings; open FILE,"< mail.fil"; my @oralines=<FILE>; close FILE; #print @oralines; open FILE,"<THIDSerrs"; my @errs = <FILE>; close FILE; #print @errs; my @subarray = (); foreach my $oralines(@oralines) { foreach my $errs(@errs) { push @subarray, $oralines if grep /$errs/, @oralines; } } print "@sub +array \n"; This doesn't return anything! <code> !/usr/local/perl-5.12.3/bin/perl use strict; use warnings; open FILE,"< mail.fil"; my @oralines=<FILE>; close FILE; #print @oralines; open FILE,"<THIDSerrs"; my @errs = <FILE>; close FILE; #print @errs; my @subarray = (); foreach my $oralines(@oralines) { foreach my $errs(@errs) { push @subarray, $oralines if grep /$errs/, @oralines; } } print "@sub +array \n";

This doesn't return anything! Looking to find common elements between two files and eliminate from one file (mail.fil)

Comment on use to files - grep one to eliminate entries from another file.
Download Code
Re: use to files - grep one to eliminate entries from another file.
by talexb (Canon) on Jan 23, 2012 at 17:56 UTC

    You need to edit your post and add code tags. Right now it's all smooshed together, so it's unreadable.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: use to files - grep one to eliminate entries from another file.
by newkendall (Initiate) on Jan 23, 2012 at 18:23 UTC

    I'm looking to find common lines between two files (mail.fil) and (THIDSerrs). I then want to eliminate the common lines from the one file, mail.fil.

    #!/usr/local/perl-5.12.3/bin/perl use strict; use warnings; open FILE,"< mail.fil"; my @oralines=<FILE>; close FILE; #print @oralines; open FILE,"<THIDSerrs"; my @errs = <FILE>; close FILE; #print @errs; my @subarray = (); foreach my $oralines(@oralines) { foreach my $errs(@errs) { push @subarray, $oralines if grep /$errs/, @oralines; } } print "@subarray \n";

    Hope this is more readable - I'm new at this.

      I'm looking to find common lines between two files
      $ man comm NAME comm - compare two sorted files line by line SYNOPSIS comm [OPTION]... FILE1 FILE2 DESCRIPTION Compare sorted files FILE1 and FILE2 line by line. With no options, produce three-column output. Column one +contains lines unique to FILE1, column two contains lines unique to FIL +E2, and column three contains lines common to both files. -1 suppress lines unique to FILE1 -2 suppress lines unique to FILE2 -3 suppress lines that appear in both files --help display this help and exit --version output version information and exit

        Trying to do all of this in Perl - management doesn't want UNIX commands so I can port this to Windows machines. Thanks.

      By doing a 'Super Search', (located at the top of any PerlMonks page here), with the words 'find common lines', you will find similiar problems and solutions.

      Only, they find lines in common rather than unique. But its not difficult to figure this the difference, I believe.

      If the THIDS file isn't too large for your memory, you could read it into a hash to check against the 'mail.fil' for differences.

      #!/usr/local/perl-5.12.3/bin/perl use strict; use warnings; open my $THIDS,"<", "THIDSerrs" or die "Could not open 'THIDSerrs' for + reading. $!"; my %data = map {$_ => 1} <$THIDS>; close $THIDS or die "Unable to close 'THIDSerrs' - reading. $!"; open my $mail, "<" , "mail.fil" or die "Could not open 'mail.fil' for +reading. $!"; open my $out, ">", "whatever.dat" or die "Unable to open 'whatever.dat +' for write. $!"; while (<$mail>) { print $out unless $data{$_}; } close $mail or die "Unable to close 'mail.fil' - reading. $!"; close $out or die "Unable to close 'whatever.dat' from writing. $!";

        Thanks. I'll work on this tonight

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://949464]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2014-12-26 08:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (168 votes), past polls