我想用命令行工具在bash中编写这个脚本,以获得乐趣。
# 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
不起作用。
由于
答案 0 :(得分:3)
您需要在致电uniq
之前进行排序,因为uniq
期望对其输入进行排序。所以:
< corpus.txt tr -d '.!@#$%^&*()-_=+' | tr -s '\t ' '\n' | sort | uniq -c | sort -rn