On Wednesday 31 October 2007 11:59, Nisse Tuta wrote:
> Hi all,
Hello,
> I have a problem i could really use some help with.
> Sorry for the long message but I find it hard to explain without
> showing.
>
> I have 2 files.
>
> File nr 1 is a reference that looks like this.
>
> 1 01:00:00:00 01:21:48:16 001
> 2 02:00:00:00 02:20:52:23 002
> 3 03:00:00:00 03:11:36:08 002
> 4 04:00:00:00 04:20:41:07 003
[ SNIP ]
> 88 08:00:00:00 08:19:10:09 032
> 89 09:00:00:00 09:21:42:06 033
> 90 10:00:00:00 10:11:04:07 033
> 91 11:00:00:00 11:20:45:22 033
> Continues a long way down.....
>
> The second file is the one that need to be changed and it looks like
> this
>
> 001 016 V C 16:09:07:12 16:09:10:11 04:15:36:07
> 04:15:39:06 002 016 V C 17:10:40:14 17:10:42:00
> 04:16:45:18 04:16:47:04 #example line
> 003 016 V C 17:11:08:02 17:11:09:20 04:16:58:08
> 04:17:00:01 004 030 V C 19:15:24:05 19:15:27:18
> 04:17:05:03 04:17:08:16 005 031 V C 06:05:21:08
> 06:05:22:24 04:17:58:01 04:17:59:17 006 031 V C
> 06:09:00:20 06:09:03:05 04:18:01:11 04:18:03:21 007 031 V C
> 06:09:03:05 06:09:03:05 04:18:03:21 04:18:03:21 007 BL
> V D 007 00:00:00:00 00:00:00:07 04:18:03:21 04:18:04:03 008
> 031 V C 06:14:21:20 06:14:24:20 04:18:59:06
> 04:19:02:06 Continues a long way down.....
>
>
> I need to change the second number in the second file.
> for example the line
> 002 016 V C 17:10:40:14 17:10:42:00 04:16:45:18
> 04:16:47:04 the 016 needs to be changed to 037
>
> The lines that dont have a number (e.g BL) should just be ignored.
>
> The reference file have the 016 in more then 1 place so the second
> match would be the timecode 17:10:40:14 is inbetween the timecodes in
> the refrence file.
> 37 17:00:00:00 17:13:15:19 016
>
> The first 4 lines should then look like this.
> 001 036 V C 16:09:07:12 16:09:10:11 04:15:36:07
> 04:15:39:06 002 037 V C 17:10:40:14 17:10:42:00
> 04:16:45:18 04:16:47:04 003 037 V C 17:11:08:02
> 17:11:09:20 04:16:58:08 04:17:00:01 004 079 V C
> 19:15:24:05 19:15:27:18 04:17:05:03 04:17:08:16
This appears to do what you want:
#!/usr/bin/perl
use warnings;
use strict;
use Fatal qw/ :void open close /;
my $file_1 = 'first_file.txt';
my $file_2 = 'second_file.txt';
open my $fh, '<', $file_1 or die "Cannot open '$file_1' $!";
my %data;
while ( <$fh> ) {
next unless /^\d/;
my ( $to, $lower, $upper, $from ) = split;
push @{ $data{ $from } }, { to => $to, lower => $lower, upper =>
$upper };
}
close $fh;
{ local ( $^I, @ARGV ) = ( '.bak', $file_2 );
while ( <> ) {
next unless my ( $number, $time ) =
/^\d+\s+(\d+)\s+\S+\s+\S+\s+(\d{2}:\d{2}:\d{2}:\d{2})\s/;
next if not exists $data{ $number };
for my $record ( @{ $data{ $number } } ) {
next if $time lt $record->{ lower } || $time gt $record->{
upper };
s/^(\d+\s+)(\d+)/ sprintf '%s%0*d', $1, length( $2 ),
$record->{ to } /e;
last;
}
print;
}
}
__END__
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/