2012-09-06 2 views
0

나는이 스크립트를 재미있게 명령 줄 유틸리티 인 bash로 작성하려고합니다.bash cmdline 토크 나이저/상대 빈도 막대 그래프 ... 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 실패 얻을. 아마도 cat -e으로 보지 못하는 문자가있을 것입니까? uniq은 라인이 무엇이고 파일이 무엇인지 어떻게 결정합니까?

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

아, 필자는 맨 페이지 만 다뤘습니다. 다음 번에는 모든 단어를 읽을거야! –