我想计算两个模式之间的重复记录,并将它们打印在模式一和二之后。
群集1 一个
一个
2个
2个
2个
集群2
2个
一个
一个
2个
集群3
一个
集群4
2个
2个
一个
群集1 2 3
集群2 2 2
群集3 1 0
群集4 1 2
我尝试过awk,但它只计算Cluster之间的行,而不是重复行。
我试过这段代码; .....
cut -d _ -f2 norm_nonnorm_merged-95-count-new.clstr | sort | uniq -c | awk '$1>1&&$0=$2 " - " $1 " times"'
打印
one - 6 times
two - 7 times
也试过
awk ' />Cluster/ { if ( f!="") {print f-1}; f=0;f1=1 } f1 { f++ } ' norm_nonnorm_merged-95.clstr
打印
5
4
1
3
答案 0 :(得分:1)
更长形式的perl脚本解决方案:
use strict;
use warnings;
my %allkeys;
my @clusters;
while (<DATA>) {
chomp;
if (/^Cluster/) {
push @clusters, [$_, {}];
} else {
$clusters[-1][1]{$_}++;
$allkeys{$_}++;
}
}
my @sortedkeys = sort keys %allkeys;
for my $cluster (@clusters) {
print "$cluster->[0] " . join(' ', map {$cluster->[1]{$_} // 0} @sortedkeys) . "\n";
}
__DATA__
Cluster 1
one
one
two
two
two
Cluster 2
two
one
one
two
Cluster 3
one
Cluster 4
two
two
one
输出:
Cluster 1 2 3
Cluster 2 2 2
Cluster 3 1 0
Cluster 4 1 2
答案 1 :(得分:1)
这适用于Python:
txt='''\
Cluster 1
one
one
two
two
two
Cluster 2
two
one
one
two
Cluster 3
one
Cluster 4
two
two
one
'''
import re
for t in re.findall(r'(Cluster \d)(.*?)(?=Cluster|$)', txt, re.S):
print t[0],t[1].count('one'), t[1].count('two')
打印:
Cluster 1 2 3
Cluster 2 2 2
Cluster 3 1 0
Cluster 4 1 2
(未经测试......)
import re
fn1='one.txt'
fn2='two.txt'
fn3='one-two.txt'
with open(fn1, 'w') as f1, open(fn2, 'w') as f2, open(fn3, 'w') as f3:
for t in re.findall(r'(Cluster \d+)(.*?)(?=Cluster|$)', txt, re.S):
one=t[1].count('one')
two=t[1].count('two')
out= '{} {} {}\n'.format(t[0], one, two)
if one and two:
f3.write(out)
elif one:
f1.write(out)
elif two:
f2.write(out)