in reply to Joining separate data files to make one.
Here's another way to do it:
#!perl use 5.10.0; use strict; use warnings; my %merged = (); my $index = 0; map { open my $file, '<', $_ or die $!; map { $merged{$_->[0]} //= [ qw{null} x 3 ]; $merged{$_->[0]}[$index] = $_->[1]; } map { [ m{ \A ( \S+ \s \S+ \s \S+ \s \S+ ) \s ( \S+ ) \z }msx ] } map { chomp; $_ } (<$file>); close $file; ++$index; } qw{gravity magnetics bathymetry}; say join(' ', $_, @{$merged{$_}}) for sort keys %merged;
I put the script in a file called geo_file_join.pl and made some short test files:
ken@Miranda ~/c/_/tmp $ cat gravity 2010-10-01 00:00:03 lat1 long1 grav1 2010-10-02 00:00:05 lat2 long2 grav2 2010-10-03 00:00:07 lat3 long3 grav3 ken@Miranda ~/c/_/tmp $ cat magnetics 2010-10-02 00:00:05 lat2 long2 mag1 2010-10-03 00:00:07 lat3 long3 mag2 2010-10-04 00:00:09 lat4 long4 mag3 ken@Miranda ~/c/_/tmp $ cat bathymetry 2010-10-03 00:00:07 lat3 long3 bath1 2010-10-04 00:00:09 lat4 long4 bath2 2010-10-05 00:00:01 lat3 long3 bath3
Here's the output:
ken@Miranda ~/c/_/tmp $ geo_file_join.pl 2010-10-01 00:00:03 lat1 long1 grav1 null null 2010-10-02 00:00:05 lat2 long2 grav2 mag1 null 2010-10-03 00:00:07 lat3 long3 grav3 mag2 bath1 2010-10-04 00:00:09 lat4 long4 null mag3 bath2 2010-10-05 00:00:01 lat3 long3 null null bath3
Assuming your latitudes and longitudes are in some sortable format, this will sort by the first 4 fields (i.e. date, time, latitude and longitude).
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Joining separate data files to make one.
by msexton (Initiate) on Oct 07, 2010 at 09:40 UTC | |
by afoken (Chancellor) on Oct 07, 2010 at 15:10 UTC | |
by kcott (Archbishop) on Oct 07, 2010 at 18:19 UTC | |
by msexton (Initiate) on Oct 08, 2010 at 12:09 UTC |
In Section
Seekers of Perl Wisdom