test
[ws10smt.git] / extools / merge_lines.pl
blob8711e4ce789b27de1eb6960dc5e41a817db39a04
1 #!/usr/bin/perl -w
2 use strict;
4 if (scalar @ARGV < 2) {
5 die "Usage: $0 file1.txt file2.txt ...\n\n Concatenate the nth line of each input file. All files\n must be the same length.\n\n";
8 my @fhs=();
9 for my $file (@ARGV) {
10 my $fh;
11 open $fh, "<$file" or die "Can't read $file: $!\n";
12 push @fhs, $fh;
15 my $first = shift @fhs;
17 while(my $x = <$first>) {
18 my $ind = 0;
19 chomp $x;
20 my @fields = ($x);
21 for my $fh (@fhs) {
22 $ind++;
23 $x = <$fh>;
24 die "ERROR: Mismatched number of lines: $ARGV[$ind]\n" unless $x;
25 chomp $x;
26 push @fields, $x;
28 print join ' ||| ', @fields;
29 print "\n";
31 my $ind = 0;
32 for my $fh (@fhs) {
33 $ind++;
34 my $x=<$fh>;
35 die "ERROR: $ARGV[$ind] has extra lines!\n" if $x;
38 exit 0;
40 for my $fh (@fhs) {
41 close $fh;