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

I am trying to create a script that uses three .txt files. file 1 has a list of names that searches file 2. If the names are in file two it copies to file three. Can anyone help wit htis. I can open and search, but not from another file and I cannot copy.

Replies are listed 'Best First'.
Re: Seek, Copy & Paste
by KurtSchwind (Chaplain) on Nov 25, 2007 at 06:50 UTC

    I'm not sure what OS you are in. But there is a unix command that does this for you.

    comm -1 a.txt b.txt > c.txt

    --
    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

      And (somewhat amazingly) you can do the same on Win32 from the command line although it does take 3 more keystrokes.....

      findstr /g:a.txt b.txt > c.txt
      Keep in mind that the unix comm command requires its input files to be sorted first.

      Maybe better to use grep for that:

      grep -f file_1 file_2 > file_3
Re: Seek, Copy & Paste
by sh1tn (Priest) on Nov 25, 2007 at 09:58 UTC
    cat filename_1 filename_2 | perl -ne '$_{$_}++ && print' -
    or just
    perl -ne '$_{$_}++ && print' filename_1 filename_2


Re: Seek, Copy & Paste
by apl (Monsignor) on Nov 25, 2007 at 12:42 UTC
    I can see the need for doing this in more than a one-liner. If you show us what you've already written, we can help you move it towards completion.
Re: Seek, Copy & Paste
by dwhite20899 (Friar) on Nov 26, 2007 at 00:28 UTC
    Sounds like a homework problem, so I won't post code.

    Read the File1 names into the keys of a hash, read the File2 names in and check each to see if it is defined as a hash key, write the name to File3.