Hello

I have at least 3 files I need to combine into one. However, I want to take the first line of each file and print it to the new file, then the second line, and so on.

I wrote a script to do this however the output is incorrect. Instead of getting something like this:

file1::line1 file2::line1 file3::line1 file1::line2 file2::line2 file3::line2 ...
I get this:
file1::line12 file2::line12 file3::line12 file1::line1 file2::line1 file3::line1 file1::line1 file2::line1 file3::line1 ...
Please Help Me,

Thanx,

Roger

#!/usr/bin/perl -w # These files have this format: # filename::line number (e.g., 1.txt::1) my @files = ( "1.txt", "2.txt", "3.txt", ); my $total_strings = 9; my $cur_pos = 0; my $next_pos = 0; open(MIXED, ">mixed.txt") || die "(x) Failed: mixed.txt - $!\n"; for (my $cur_string=1; $cur_string <= $total_strings; $cur_string++) + { print "\nCurrent String: $cur_string of $total_strings\n"; foreach my $file (@files) { print " Opening File: $file\n"; print " Current Position is: $cur_pos\n"; open(FILE, $file) || die "(x) Failed: $file - $!\n"; while(<FILE>) { seek(FILE, $cur_pos, 1); $string = $_; $next_pos = tell(FILE); } close(FILE); print " Read string: $string"; print MIXED $string; print " Next Position is: $next_pos\n\n"; } $cur_pos = $next_pos; } close(MIXED);

In reply to combine multiple files into one (line by line) by Anonymous Monk

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.