in reply to Use 2 files and print each line of each one side-by-side

A simple one-liner will do for this:

C:\test>type a.txt A00001 A00002 A00003 A00004 A00005 A00006 A00007 A00008 A00009 A00010 C:\test>type b.txt B00001 B00002 B00003 B00004 B00005 B00006 B00007 B00008 B00009 B00010 C:\test>perl -nle"printf qq[%10s\t%10s], $_, scalar <STDIN>" a.txt <b. +txt A00001 B00001 A00002 B00002 A00003 B00003 A00004 B00004 A00005 B00005 A00006 B00006 A00007 B00007 A00008 B00008 A00009 B00009 A00010 B00010

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re: Use 2 files and print each line of each one side-by-side (One-liner)
  • Download Code

Replies are listed 'Best First'.
Re^2: Use 2 files and print each line of each one side-by-side (One-liner)
by jellisii2 (Hermit) on Feb 18, 2014 at 20:15 UTC
    For relative values of simple...
      For relative values of simple...

      Huh? Relative to what? A plank.

      It's two well-known switches and a print statement.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I seldom do oneliners myself, and never anything this clever, but let me attempt to noodle out what's going on here as an exercise for myself. None of this should come as a surprise to those who have serious depth in perl, particularly those who do lots of CLI work, but it will be enlightening to me.

        -n forces the program to loop while there's still input from a.txt. I assume this also takes into account input from <STDIN> as well in the event b.txt is longer than a.txt, but I'm unclear on that point.

        -l tells perl that a carriage return is the record separator, and chomps each line from a.txt, and sets the output record separator to the same, so each line printed should have a carriage return. This may also chomp input from b.txt, but I'm unclear on that.

        The formatting of the print statement is pretty obvious to me, but for completion's sake, the format is 10 characters (with right padding of spaces if the string is short), a tab, and another 10 characters.

        $_ is the current line from a.txt, and will be used left of the tab in the formatted print.

        I don't understand why scalar is in place for the <STDIN> that is ingesting b.txt from the shell's redirect. Assuming this works as advertised (not that I have any real doubt), it will be used in right of the tab in the formatted print.

        To those of us (it may just be me, who knows) who don't often use the CLI, no, it's not quite as simple as it would seem to you. I get that "knowing the tool" is important, and strive to learn more as I go, but the reality is that there's a LOT baked into perl to learn. Between this fact, the habit to "stick with what works", and that my job nor hobby is %100 perl, I freely admit my education/depth/knowledge may be a bit lopsided. I think this would apply to a majority of the monks in the monastery to some degree.

        Thank you for the enlightenment.