2012-09-06 22 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。下一次,我會讀每一個字! –