spek_'s Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
1 (0 per day)
Joined:
12-March 12
Profile Views:
56
Last Active:
User is offline Mar 12 2012 10:59 AM
Currently:
Offline

Previous Fields

Dream Kudos:
0
Icon   spek_ has not set their status

Posts I've Made

  1. In Topic: Compare multiple files and output differences

    Posted 12 Mar 2012

    As far as I could tell, you are trying to do something like the following. It reads a.txt, which contains a list of other filenames like this:

    b.txt
    c.txt
    d.txt
    
    


    ...then b.txt, c.txt and d.txt have entries something like this:

    hello from b.txt
    this line shouldn't match
    this line hello should match
    shouldn't
    hello should match
    
    


    ...then you want to grab all of the lines from b, c and d, and save all those lines that match $pattern, removing duplicates:

    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    print "Enter filename:";
    chomp( my $list_filename = <STDIN> );
    
    open my $list_fh, '<', $list_filename 
      or die "Can't open the file that contains the other filenames!: $!";
    
    my @storage; # store the lines in here
    my $pattern = qr/hello /;
    
    while ( my $inner_filename = <$list_fh> ){
    
      chomp $inner_filename;
    
      # ensure the files actually exist (-e)
    
        if ( -e $inner_filename ){
    
            open my $inner_file_fh, '<', $inner_filename
              or die "Can't open $inner_filename!: $!";
    
            print "$inner_filename\n";    # Print the file name to indicate the different output
    
            while ( my $inner_line = <$inner_file_fh>) {
    
                chomp $inner_line;
    
                if ( $inner_line =~ /$pattern/ ){
    
                    print "$inner_line\n";
                    push @storage, $inner_line; 
                }
            }
            close $inner_file_fh;
        }
    }
    close $list_fh;
    
    # combine all lines removing duplicates
    # see: perldoc -q duplicate
    
    my ( @unique, %seen );
    
    foreach my $elem ( @storage ){
        next if $seen{ $elem }++;
        push @unique, $elem;
    }
    
    print "\n\nResults:\n\n";
    print join( "\n", @unique );
    
    


    I'll leave it up to you do do any further output to other files :)

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

spek_ hasn't added any friends yet.

Comments

spek_ has no profile comments yet. Why not say hello?