遵循 Perl 方法。
- 注1:写得不是很好,但可以正常工作
- 注2:我的回答是基于这样的考虑,即“start_time”和“end_time”不是字面意义上的字符串,而是某种时间戳或其他
你去:
#!/usr/bin/perl
use warnings;
use strict;
my @waiting; #here we will keep track of the order
my %previous; #here we will save previous rows that still can't be printed
open (my $IN,'<','file.txt') or die "$!"; #assuming that your data is in file.txt
while (<$IN>) {
chomp;
my ($id,$time)=split/ /;
if (exists $previous{$id}) { #if this is the end_time
$previous{$id}->[1]=$time;
if ($waiting[0]==$id) { #if we are not waiting for another row's end_time
my $count=0;
for (@waiting) { #print anything you have available
last if !defined $previous{$_}->[1];
print join(' ',$x,@{$previous{$_}}),"\n";
delete $previous{$_};
$count++;
}
shift @waiting for 1..$count;
}
}
else { #if this is the start_time
push @waiting,$id;
$previous{$id}=[$time,undef];
}
}
close $IN;