我有这样的话:
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma:
alpha
beta
gamma
epsilon: alpha
我想连接行只包含一个单词后跟冒号,其中包含不包含冒号的行:
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma: alpha, beta, gamma
epsilon: alpha
答案 0 :(得分:1)
这是一个快速而又脏的版本:
#!/usr/bin/perl
use strict;
use warnings;
my $prevLine = "";
my @others;
for(<DATA>) {
chomp;
if (/:\s*$/) { # lines ends with : and potential space after
$prevLine = $_;
} elsif (!/:/) { # line doesn't contain ':'
push(@others, $_);
} elsif ($prevLine eq "") { # this is a "x: y, z" line, nothing in buffer
print $_ . "\n";
} else { # this is a "x: y, z" line, with a previous line in buffer
print $prevLine . join(", ", @others) . "\n" . $_ . "\n";
}
}
__DATA__
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma:
alpha
beta
gamma
epsilon: alpha
<强>输出:强>
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma: alpha, beta, gamma
epsilon: alpha
答案 1 :(得分:1)
这应该与RCs代码相同,但稍微缩短一点:
my @lines;
while(<FILE>) {
chomp;
if(m/^\w+:\s(\w+(,\s)?)*$/) {
push @lines, $_;
} else {
$lines[$#lines] .= ", " unless($lines[$#lines] =~ m/^\w+:\s?$/);
$lines[$#lines] .= $_;
}
}
print join "\n", @lines;
答案 2 :(得分:1)
如果输入在文件中作为脚本的参数提供,并且稍微短一点,这也有效:
$/=$.;$_=<>;s/\s?\n/, /g;s/, (\w+:),?/\n$1/g;s/, $/\n/;print
它不具有可读性,但适用于strict
和warnings
。
输出:
alpha: beta
beta: alpha, beta
omega: beta, gamma, alpha
gamma: alpha, beta, gamma
epsilon: alpha
它可能会进一步缩短。 -ape
或#!perl -ap
将是一个良好的开端。