我有文本文件是这样的:重新排列列的元素和显示计数
i
am
fine
how
are
you
what
i
how
are
我需要的输出象下面这样:
i : 2
am : 1
fine : 1
how : 2
are : 2
you : 1
what : 1
有可能的话很多repititions: 我怎么可能使用shell脚本或awk来执行此操作?
我有文本文件是这样的:重新排列列的元素和显示计数
i
am
fine
how
are
you
what
i
how
are
我需要的输出象下面这样:
i : 2
am : 1
fine : 1
how : 2
are : 2
you : 1
what : 1
有可能的话很多repititions: 我怎么可能使用shell脚本或awk来执行此操作?
sort | uniq -c
它对它进行排序,计数在默认行之前。这会起作用吗?
awk '{ count[$1]++ }
END { for (a in count) printf("%s : %d\n", a, count[a]) }' filename
awk
有关联数组,所有变量都被初始化为0,所以上面的工作方式和预期一样。
在Perl:
perl -le'while (<>){ chomp; $seen{$_}++}; print map { $_ . " : " . $seen{$_} } keys %seen'
哎呀,我忘了增量! (几个月后......):) – Ether 2010-07-18 17:19:41
@OP,如果你想保留的顺序
awk ' { a[$0]++; d[NR]=$0 }
END{
for(i=1;i<=NR;i++){
if(! (d[i] in p) ){
print a[d[i]],d[i]
p[d[i]]
}
}
} ' file
输出
$ ./shell.sh
2 i
1 am
1 fine
2 how
2 are
1 you
1 what
是这部作品:) – Vijay 2010-02-01 06:59:05
这人应该是接受的答案恕我直言。 – Ether 2010-02-01 07:16:16