in reply to Content of one file to another file if a filed matches

$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 $

Replies are listed 'Best First'.
Re^2: Content of one file to another file if a filed matches
by Lotus1 (Vicar) on Mar 09, 2017 at 16:49 UTC

    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

        Your approach of appending the value on key collisions was new to me. I've been trying to improve my one-liner skills so this was a good chance to practice figuring out how it worked.

Re^2: Content of one file to another file if a filed matches
by misc (Friar) on Mar 11, 2017 at 20:59 UTC

    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.

Re^2: Content of one file to another file if a filed matches
by sm81 (Initiate) on Mar 09, 2017 at 17:40 UTC
    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". ;-)