Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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:
I get this:file1::line1 file2::line1 file3::line1 file1::line2 file2::line2 file3::line2 ...
Please Help Me,file1::line12 file2::line12 file3::line12 file1::line1 file2::line1 file3::line1 file1::line1 file2::line1 file3::line1 ...
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: combine multiple files into one (line by line)
by danger (Priest) on Apr 05, 2001 at 23:31 UTC | |
|
Re: combine multiple files into one (line by line)
by ton (Friar) on Apr 05, 2001 at 22:39 UTC | |
by Anonymous Monk on Apr 05, 2001 at 22:53 UTC | |
by extremely (Priest) on Apr 05, 2001 at 23:36 UTC | |
by jeroenes (Priest) on Apr 05, 2001 at 23:49 UTC | |
by ton (Friar) on Apr 06, 2001 at 00:01 UTC | |
|
Re: combine multiple files into one (line by line)
by thabenksta (Pilgrim) on Apr 05, 2001 at 22:39 UTC |