bash cmdline tokenizer / relative frequency histogram ... uniq失败

时间:2012-09-06 04:43:33

标签: bash

我想用命令行工具在ba​​sh中编写这个脚本,以获得乐趣。

# make the corpus
echo -e "this \t is   a \n cor-pus;  \nthis  \t\nis \n\t sparta. \n" > corpus.txt

# munge, collapse whitespace, tokenize
cat corpus.txt | tr -d '.!@#$%^&*()-_=+' | tr '\t' ' ' | tr '\n' ' ' | tr -s ' ' | fmt -1 | uniq -c | sort -rn

我希望

   2 this
   2 is
   1 a
   1 corpus
   1 sparta

但是我得到了

   1 this
   1 is
   1 a
   1 corpus
   1 this
   1 is
   1 sparta
管道uniq

fmt -1失败。也许有{e = 1}}我没有看到的eof chars? cat -e如何确定哪些行和什么是文件?

uniq做了我期望的事情,所以我不知道为什么我脚本中的echo a a b | fmt -1 | uniq不起作用。

由于

1 个答案:

答案 0 :(得分:3)

您需要在致电uniq之前进行排序,因为uniq期望对其输入进行排序。所以:

< corpus.txt tr -d '.!@#$%^&*()-_=+' | tr -s '\t ' '\n' | sort | uniq -c | sort -rn