Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Commit little utility for merging tab-delimited text file tables.
Probably there's some existing tool out there, but this is what I wrote to merge new locales into the Interchange Standard demo locale.txt file. Maybe it'll come in handy for others.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/local/bin/perl | ||
|
||
# merge-tab-files | ||
# by Jon Jensen <jon@endpoint.com> | ||
# 2008-04-11 | ||
# | ||
# A tool for merging multiple tab-delimited files. | ||
# Each tab-delimited file's first line is expected to contain the column names. | ||
# | ||
# Usage: merge-tab-files $file1 [$file2 ...] | ||
|
||
use strict; | ||
use warnings; | ||
use List::MoreUtils qw( uniq ); | ||
#use Data::Dumper; | ||
|
||
my %data; | ||
my @all_cols; | ||
|
||
for my $file (@ARGV) { | ||
open my $in, '<', $file or die "Couldn't open file $file: $!\n"; | ||
$_ = <$in>; | ||
chomp; | ||
my @cols = split /\t/, $_, -1; | ||
push @all_cols, @cols; | ||
while (<$in>) { | ||
chomp; | ||
my @row = split /\t/, $_, -1; | ||
my $key = $row[0]; | ||
# Don't want to clobber duplicate column definitions if the new one is empty | ||
$data{$key}{$cols[$_]} ||= $row[$_] for 1..$#cols; | ||
} | ||
close $in; | ||
} | ||
|
||
@all_cols = uniq @all_cols; | ||
print join("\t", @all_cols), "\n"; | ||
|
||
#print Dumper(\%data); | ||
|
||
no warnings 'uninitialized'; | ||
|
||
for my $key (sort keys %data) { | ||
print join("\t", $key, map { $data{$key}{$_} } @all_cols[1..$#all_cols]), "\n"; | ||
} |