给定一个带有空格分隔单词的 .txt 文件,例如:
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}'
我在控制台中得到以下输出:
1 Bastard
1 Esope
1 holly
1 the
2 But
2 is
2 where
如何进入打印到 myFile.txt ? 我实际上有 300.000 行和近 200 万字。最好将结果输出到文件中。
编辑:使用的答案(@Sudo_O):
$ awk '{a[$1]++}END{for(k in a)print a[k],k}' RS=" |\n" myfile.txt | sort > myfileout.txt