工作上用 Shell 的频率是很高的,哪怕现在有了 Ansible 或者其他配置工具,Shell 仍是一个以 Linux 作为工作环境的同学的必备技能。 之前写过 GitHub 上的 Pure Bash Bible 的博客,看到 LeetCode 上的 Shell 题目好久不更新了,只有 4 道,今天记录一下题解。
192. Word Frequency 链接到标题 统计文本文件中单词出现次数,倒序输出。
words.txt
the day is sunny the the the sunny is is 利用 tr sort uniq awk 解决。
# Read from the file words.txt and output the word frequency list to stdout. cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -rn | awk '{ print $2, $1 }' 193.