I have two input files (rmsd1.xvg and rmsd2.xvg). I want to write them into output file after selecting a different column from each input file. My input files and code are the following:
rmsd1.xvg: # GROup of MAchos and Cynical Suckers # @ title "RMSD" @ xaxis label "Time (ps)" @ yaxis label "RMSD (nm)" 1.0000000 0.0000009 20.0000000 0.1478001 40.0000000 0.1648600 60.0000000 0.1645018 80.0000000 0.1786710 100.0000000 0.2115960
rmsd2.xvg: # GROup of MAchos and Cynical Suckers # @ title "RMSD" @ xaxis label "Time (ps)" @ yaxis label "RMSD (nm)" 1.0000000 0.1000009 20.0000000 0.2478001 40.0000000 0.3648600 60.0000000 0.4645018 80.0000000 0.5786710 100.0000000 0.6115960
My code: #!/usr/bin/perl use strict; use warnings; # Open file1 to read open my $input_file1, '<', "rmsd1.xvg" or die qq{Failed to open "rmsd1 +.xvg" for writing: $!}; # Open file2 to read open my $input_file2, '<', "rmsd2.xvg" or die qq{Failed to open "rmsd2 +.xvg" for writing: $!}; # Open new file to write open my $out_file, '>', "out_file.xvg" or die qq{Failed to open "out_f +ile.xvg" for writing: $!}; while(<$input_file1>) { next if /(^\s*$)|(^#)|(^@)/; my @columns1 = split; print $out_file join("\t", $columns1[0],$columns1[1], "\n"); } while(<$input_file2>) { next if /(^\s*$)|(^#)|(^@)/; my @columns2 = split; print $out_file join("\t", $columns2[1]), "\n"; } close($input_file1); close($input_file2); close($out_file);
My code gives me a output as the following.
Output file: 1.0000000 0.0000009 20.0000000 0.1478001 40.0000000 0.1648600 60.0000000 0.1645018 80.0000000 0.1786710 100.0000000 0.2115960 0.1000009 0.2478001 0.3648600 0.4645018 0.5786710 0.6115960
Whereas i want to get a output as the following. That is all columns should be side by side. How can i get this output?
Requested: 1.0000000 0.0000009 0.1000009 20.0000000 0.1478001 0.2478001 40.0000000 0.1648600 0.3648600 60.0000000 0.1645018 0.4645018 80.0000000 0.1786710 0.5786710 100.0000000 0.2115960 0.6115960

In reply to Adding some columns into a file from two files using perl by perlselami

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.