使用perl在文件中使用不常见的项目组织公共项目

时间:2014-08-01 02:24:29

标签: perl

我有一个包含3列这样的数据的文件。这可能有数千行。列。

A    6:23   started 
A    7:12   completed
B    9:14   started
B   10:21   completed
C   12:33   started
C   13:44   completed

我希望获得输出,例如使用perl

First_col   start_time  end_time  ( 3 columns)
A           6:23        7:12
B           9:14        10:21
C           12:33       13:44

2 个答案:

答案 0 :(得分:0)

您可以这样替换:

s/([A-Z])( [0-9]{1,2}:[0-9]{1,2}) started \1((?2)) completed/$1$2$3/g

online demo

或更短,你可以删除你不想要的东西:

s/ started [A-Z]| completed//g

答案 1 :(得分:0)

#!/usr/bin/perl -w
use strict;
use 5.014;

my $idxLine = 0;
my @coll;
my $entry;
while (<>) {
    next if m/^$/; # Skip empty lines.
    if (++$idxLine % 2) {
        $entry = CEntry->new($_);
    }
    else {
        $entry->setCompleted($_);
        push(@coll, $entry);
    }
}

printf("%s %s %s\n", $_->{name}, $_->{start}, $_->{completed}) foreach @coll;

package CEntry;
use strict;
use 5.014;
sub new {
    my ($class, $name, $start) = ($_[0], (split(/\s/, $_[1]))[0,1]);
    return bless({name => $name, start => $start}, $class);
}
sub setCompleted {
    my ($self, $completed) = ($_[0], (split(/\s/, $_[1]))[1]);
    $self->{completed} = $completed;
}

执行命令

$ cat data
A 6:23 started

A 7:12 completed

B 9:14 started

B 10:21 completed

C 12:33 started

C 13:44 completed
$ ./a.pl data
A 6:23 7:12
B 9:14 10:21
C 12:33 13:44
$ ./a.pl < data
A 6:23 7:12
B 9:14 10:21
C 12:33 13:44