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);
In reply to combine multiple files into one (line by line) by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |