1

以下のようなファイルがあります。

101 start_time
102 start_time
101 end_time
103 start_time
103 end_time
102 end_time
104 start_time
104 end_time
102 start_time
102 end_time

以下のような出力ファイルが必要です。

101 start_time end_time
102 start_time end_time
103 start_time end_time
104 start_time end_time
102 start_time end_time

基本的な sed または awk 操作、または perl でどのように実行できますか? 助けてください!

4

3 に答える 3

2

どうですか:

awk '$1 in a{ print $1, a[$1], $2; delete a[$1]; next} {a[$1] = $2}' input
于 2013-03-13T22:42:29.313 に答える
0
perl -anE'say "@F end_time" if $F[1] eq "start_time"'
于 2013-03-13T23:23:45.473 に答える
0

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;
于 2013-03-14T00:55:18.850 に答える