2013-03-24 31 views
0

給出一個.txt文件用空格分隔的詞語,如:Awk:CJK字符有什麼問題? #Korean

But where is Esope the holly Bastard 
But where is 생 지 옥 이 군 
지 옥 이 
지 옥 
지 
我 是 你 的 爸 爸 ! 
爸 爸 ! ! ! 
你 不 會 的 ! 

而且awk的功能

cat /pathway/to/your/file.txt | tr ' ' '\n' | sort | uniq -c | awk '{print $2" "$1}' 

我得到以下輸出在我的控制檯對於韓文單詞無效(英文和中文空格分隔的單詞有效)

생 16 
Bastard 1 
But 2 
Esope 1 
holly 1 
is 2 
the 1 
where 2 
不 1 
你 2 
我 1 
是 1 
會 1 
爸 4 
的 2 

如何讓它適用於韓語單詞? 注:我實際上有300.000行和接近2百萬字。


編輯:二手答案:

$ awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" myfile.txt | sort > myfileout.txt 

回答

2

awk腳本可以輕鬆地處理這一點,會比你目前的管道更爲高效:

$ awk '{a[$1]++}END{for(k in a)print k,a[k]}' RS=" |\n" file 
옥 3 
Bastard 1 
! 5 
爸 4 
군 1 
지 4 
But 2 
會 1 
你 2 
the 1 
是 1 
不 1 
이 2 
Esope 1 
的 2 
holly 1 
where 2 
생 1 
我 1 
is 2 

如果你想存儲將結果導入另一個文件中可以使用重定向,如:

$ awk '{a[$1]++}END{for(k in a)print k,a[k]}' RS=" |\n" file > outfile 
+0

很簡單:太棒了!謝謝。 – Hugolpz 2013-03-24 16:29:17

+0

這個答案是[在detals裏解釋](http://stackoverflow.com/questions/15598935/awk-how-to-output-result-into-myfile-txt-words-frequency/15600106?#15600106) – Hugolpz 2013-03-24 16:37:57