我有数据如下:
MGW: VMG110
836-16
836-18
836-19
336-20
836-23
MGW: VMG120
3802-1
3802-2
3802-3
3456-1
3456-4
所需的输出是:
VMG110:836-16&-18&&-20&-23
VMG120:3802-1&&-3&3456-1&-4
给定数据 -1,-2,-3,-4 总是按排序方式
输出中缺少 -19...而不是 -18&&-20 存在..其中 && 表示 18 到 20..请提供相同的内容。
答案1
Perl 解决方案。输出的第一行与您的不同(在所需的输出中缺少-
后面?)。&
#!/usr/bin/perl
use warnings;
use strict;
my $first = 1;
my @line;
sub output {
print join('&', @line), "\n" unless $first;
}
my $previous = q();
while (<>) {
chomp;
if (s/.*: //) {
output();
print "$_:";
$previous = q();
@line = ();
} else {
my ($prefix, $suffix) = split /-/;
if ($prefix ne $previous) {
push @line, "$prefix-$suffix";
} else {
push @line, "-$suffix";
}
$previous = $prefix;
}
undef $first;
}
output(); # Don't forget to output the last line.
答案2
awk -F '-|: ' '
function printit( sep,key) {
sep = ""
for (key in data) {
printf "%s%s", sep, data[key]
sep = "&"
}
print ""
delete data
}
$1 == "MGW" {
if (key1) printit()
key1 = $2
printf "%s:", key1
next
}
!data[key1,$1] {
data[key1,$1] = $0
next
}
{data[key1,$1] = data[key1,$1] "&-" $2}
END {printit()}
' file
VMG110:836-19&-23
VMG120:3802-1&-2&-3&3456-1&-2
VMG119:3248-1&-2&3802-8&-9&-10&-11&-12&-13&3457-18&-19&-20