2012-09-06 155 views
0

我想用bash命令行utils编写这个脚本,为了好玩。bash cmdline tokenizer /相对频率直方图... uniq失败

# 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 '[email protected]#$%^&*()-_=+' | tr '\t' ' ' | tr '\n' ' ' | tr -s ' ' | fmt -1 | uniq -c | sort -rn 

我期待

2 this 
    2 is 
    1 a 
    1 corpus 
    1 sparta 

,但我得到的管道fmt -1

1 this 
    1 is 
    1 a 
    1 corpus 
    1 this 
    1 is 
    1 sparta 

uniq失败。也许有eof字符我看不到cat -euniq如何决定线和什么是文件?

echo a a b | fmt -1 | uniq做我也期待它,所以我不知道为什么... | fmt -1 | uniq | ...在我的脚本不起作用。

感谢

+3

'tr'\ t'''| tr'\ n'''| tr -s'''可以写成'tr -s'\ t \ n'''' –

回答

3

你需要理清你打电话之前uniq作为uniq预计其输入进行排序。所以:

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

啊,我只是撇去了man page。下一次,我会读每一个字! –