8/27/2007

汇总统计

1.汇总
样本文件,这里暂时称为 file1,内容如下:
a
a
b
b
a
b
c
a
d
b
b
b
d
a
c
--------------------
执行以下命令:
cat file1 | sort // 按照字母顺序排序
cat file1 | sort | uniq // 按照字母顺序排序且去除重复项
cat file1 | sort | uniq -c // 按照字母顺序排序且去除重复项,在每行前列出重复项的个数
cat file1 | sort | uniq -c | sort -rn // 在每行前列出重复项的个数,按个数排序且去除重复项

2.单词统计
cat file3
cat file3 | sed -e "s/ /\n/g"
cat file3 | sed -e "s/ /\n/g" | sort
cat file3 | sed -e "s/ /\n/g" | sort | uniq -c
cat file3 | sed -e "s/ /\n/g" | sort | uniq -c | sort -rn

取头尾

cat file3 | sed -e "s/ /\n/g" | sort | uniq -c | sort -rn | head
cat file3 | sed -e "s/ /\n/g" | sort | uniq -c | sort -rn | head -n5
cat file3 | sed -e "s/ /\n/g" | sort | uniq -c | sort -rn | tail
cat file3 | sed -e "s/ /\n/g" | sort | uniq -c | sort -rn | tail -n5

统计词(总数,不重复的个数)有好几种办法

cat file3 | sed -e "s/ /\n/g" | sort | uniq | wc -l
cat file3 | sed -e "s/ /\n/g" | wc -l

cat file3 | wc
cat file3 | wc | cut -d" " -f2
cat file3 | wc -w
cat file3 | sed -e "s/ /\n/g" | wc -l