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

I am new to Perl and looking for help I have two files.

file 1.txt:

A Rahul 30 27 M Radha 45 S Ayaan 2 30 T Rayan 72 45 Z Sindh 4 90

file 2.txt:

A Apple B Ball C Cat D Dog F Frog G Goat H Hen I Ink J Jar K Kite L Lion M Mango O Orange P Parrot Q Queen R Rose S Sea T Train U Umbrella V victory W Watch X X-ray Y Yatch Z Zebra
I need output like this:
A Rahul 30 27 Apple M Radha 45 Mango S Ayaan 2 30 Sea T Rayan 72 45 Train Z Sindh 4 90 Zebra

Any help here would highly be appreciated.

Let me know if you need any other information.

Replies are listed 'Best First'.
Re: Content of one file to another file if a filed matches
by kcott (Archbishop) on Mar 09, 2017 at 08:33 UTC

    G'day sm81,

    Welcome to the Monastery.

    The file formats are lost because you've posted the file contents as paragraph text. Please wrap all code and data in <code>...</code> tags.

    Maybe it's lost amongst the data, but I can't see what code you've tried or what output you're currently getting and how that differs from what you actually want.

    Please see "Writeup Formatting Tips" and "How do I post a question effectively?" for more details on these issues.

    — Ken

Re: Content of one file to another file if a filed matches
by Marshall (Canon) on Mar 09, 2017 at 09:09 UTC
    Aside from the formatting problems, this is obviously a homework assignment.

    My take on your input/output file(s):

    File1: A Rahul 30 27 M Radha 45 S Ayaan 2 30 T Rayan 72 45 Z Sindh 4 90 File2: A Apple B Ball C Cat D Dog F Frog G Goat H Hen I Ink J Jar K Kite L Lion M Mango O Orange P Parrot Q Queen R Rose S Sea T Train U Umbrella V victory W Watch X X-ray Y Yatch Z Zebra Desired Output: A Rahul 30 27 Apple M Radha 45 Mango S Ayaan 2 30 Sea T Rayan 72 45 Train Z Sindh 4 90 Zebra
    Your prof must have recently talked about techniques to do this? Otherwise this wouldn't be a homework assignment. Please attempt this on your own and let us know what the problems are. You won't learn if some Monk just posts the answer.

    If you show some effort on your part, you will get a heck of a lot more help. Ok, one HUGE hint. Read File2 and make hash of its contents. Read File1 line by line and print the desired output for each input line. Show us that code and where you are having problems.

      Sure, let me try.. will post my findings.. thanks
Re: Content of one file to another file if a filed matches
by Corion (Patriarch) on Mar 09, 2017 at 11:14 UTC
Re: Content of one file to another file if a filed matches
by madtoperl (Hermit) on Mar 09, 2017 at 11:12 UTC
    You may try with below hint,
    1. Read the first file line by line and keep the Alphabet (A) and its +rest of the values(Rahul 30 27) in a hash with key/pair value. 2. Read the second file line by line and split the Alphabet and its va +lue. 3. Check if the alphabet in step 2 is available in hash formed in step + 1. If yes, print the hash key, value and second file value in new fil +e (third file).
      Thanks.. let me try ..
Re: Content of one file to another file if a filed matches
by trippledubs (Deacon) on Mar 09, 2017 at 14:25 UTC
    $cat File1 File2 | perl -lane '$h{$F[0]} .= " @F[1..$#F]"; END { print + join "\n",@h{qw(A M S T Z)}; }' Rahul 30 27 Apple Radha 45 Mango Ayaan 2 30 Sea Rayan 72 45 Train Sindh 4 90 Zebra $

      Very nice. I didn't know about the autosplit, -a, option. One small improvement, I added the letter at the beginning of each line as requested by the OP. I'm running this on a Windows server so I had to adjust the quotes to work.

      cat File1 File2 | perl -lane "$h{$F[0]} .= qq( @F[1..$#F]); END { prin +t map qq($_$h{$_}\n), qw(A M S T Z); }" A Rahul 30 27 Apple M Radha 45 Mango S Ayaan 2 30 Sea T Rayan 72 45 Train Z Sindh 4 90 Zebra
        thanks, agreed map is better

      ROTFL

      Anyone got a picture of the OP showing this solution to his prof??
      I imagine the prof's face.. O, a solution. O. OOO.

      ..."Could you explain please how this works?"
      "UUAeehm. aem. uumm."
      lol

        Anyway, I didn't want to be too harsh.
        Just had to laugh about my imagination.

        But I'm sure you can and should try your task on your own.
        To be honest it's quite basic.
        And I'd like to propose Perl was originally intended for this sort of tasks,
        working with text files (config and log files).
        So your homework is quite senseful.

        I'm also sure if you try and run into any troubles, you will get any help you need here on this forum.

        Hopefully you'll be successfull. To see a script doing what you want can also be fun and a nice affirmation.

        Good Luck..


        ...addition:
        I'd like to give you the hint that your prof will most likey test the script with other files.
        If your script gives you the desired output you posted,
        this doesn't mean it works in all cases.

      File1 is an dynamic file, contents keep on changing, will this work then as well?
        File1 is an dynamic file, contents keep on changing, will this work then as well?

        TITS: Try It To See!

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)