I am writing a script to read a text file, say a.txt, which contains a list of text file to be analyzed, say b.txt, c.txt and d.txt. Each of the a, b and c will be opened and traced to capture a certain string using pattern matching. The result will be stored in corresponding log file. After that b.log will be treat as a reference, and c.log and d.log will be compared with b.log and the missing and extra entries of them will be output to result.list. This is the code I had written so far.
#!/usr/bin/perl print "Enter filename:"; chomp($fname = <STDIN>); # Open the file which contains list of files to be opened. If unsucce +ssful, print an error message and quit. open (file1,$fname) || die "Can't Open File: $fname\n"; while (<file1>) { chomp; open (file2, $_); # Open file in the list one by one $temp = $_; # Store the file name to name the corresponding +log file print "$temp\n"; # Print the file name to indicate the different +output while (<file2>) { chomp; sub grep_pattern # Print strings which contain the pattern {foreach ($_) {if (/$pattern/) { print "$1\n"; open (file3, ">>$temp.log"); print file3 "$1\n"; # Print result in log file in append m +ode close (file3); } } } $pattern = 'hello '; # Find the pattern grep_pattern; } print "\n"; # Print a blank line to seperate the results from each + file } close (file1);
But at best the log file should not be used and probably the intermediate result should be stored in some arrays or hashes and directly be compared. But I don't know how to create different array for each of the b.txt, c.txt and d.txt automatically. Anyone can help?

In reply to Compare multiple files and output differences by prescott2006

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.