rajyalakshmi has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: problem while reading multiple files
by kennethk (Abbot) on Jul 13, 2010 at 14:07 UTC
    You've been around long enough that you should know to post code, as you did in problem while searching for a pattern in a file. Read and meditate on How (Not) To Ask A Question.

    A rough outline for your code would be:

    1. open F1
    2. open F2
    3. Slurp F2 into an array
    4. For every line in F1 (perhaps using I/O Operators in a while loop)
      1. Check if the variable containing F2 contains the line in question, perhaps using a regular expression and grep.
      2. If yes, print the line.

    If you do not know how to do any of these steps and have read the documentation, post specific questions with code (or at least pseudocode), sample input and expected output. Perlmonks is here to help you help yourself.

      I followed the steps,it solved the problem..Thanks a lot everyone
Re: problem while reading multiple files
by toolic (Bishop) on Jul 13, 2010 at 13:59 UTC
    how do we do this
    One solution is to use Perl. perlintro is a great way to begin.

    Another solution is to use the Unix utilities sort and comm.

Re: problem while reading multiple files
by roboticus (Chancellor) on Jul 13, 2010 at 13:51 UTC

    What have you tried? What part do you have trouble understanding?

    ...roboticus

Re: problem while reading multiple files
by cdarke (Prior) on Jul 13, 2010 at 14:16 UTC
    One of the most important considerations in a problem like this is volumes, that is, how much data is involved?

    The solutions appropriate when the files are a few kb is very different if the files are in Gigabytes. The number of keys in F1 is important, as is the key size.

Re: problem while reading multiple files
by suhailck (Friar) on Jul 13, 2010 at 14:38 UTC
    An awk solution

    awk 'NR==FNR{a[$0]=$0;next}$0 in a{print}' file2 file1


    ~suhail
Re: problem while reading multiple files
by JavaFan (Canon) on Jul 13, 2010 at 15:10 UTC
    Assuming order isn't important:
    $ comm -12 <(sort file1) <(sort file2)
    No perl needed.
      And a Perl solution that does the same:
      use File::Slurp; my @a1 = sort {$a cmp $b} read_file shift; my @a2 = sort {$a cmp $b} read_file shift; while (@a1 && @a2) { my $cmp = $a1[0] cmp $a2[0]; print $a1[0] if !$cmp; shift @a1 if $cmp <= 0; shift @a2 if $cmp >= 0; }
Re: problem while reading multiple files
by jwkrahn (Abbot) on Jul 13, 2010 at 18:48 UTC