![你好,我正在尝试查找文本中在 UNIX 中只出现一次的所有单词。我可以做些什么不同的事情?](https://linux22.com/image/224518/%E4%BD%A0%E5%A5%BD%EF%BC%8C%E6%88%91%E6%AD%A3%E5%9C%A8%E5%B0%9D%E8%AF%95%E6%9F%A5%E6%89%BE%E6%96%87%E6%9C%AC%E4%B8%AD%E5%9C%A8%20UNIX%20%E4%B8%AD%E5%8F%AA%E5%87%BA%E7%8E%B0%E4%B8%80%E6%AC%A1%E7%9A%84%E6%89%80%E6%9C%89%E5%8D%95%E8%AF%8D%E3%80%82%E6%88%91%E5%8F%AF%E4%BB%A5%E5%81%9A%E4%BA%9B%E4%BB%80%E4%B9%88%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8B%E6%83%85%EF%BC%9F.png)
# -- Q2 --
# Write a pipeline that finds the number of words in the book that have been used only once.
# Your pipeline should be case insensitive
echo "-- Q2 --"
onlyOnce=$(cat *txt | tr '[:upper:]' '[:lower:]' | tr -s '[:punct:][:space:]' ' '| tr ' ' '\n' | sort | uniq -ic | awk '$1 == 1 {print $2}')
echo "Words used only once:"
echo "$onlyOnce"
答案1
和awk
:
cat *txt |
tr '[:upper:]' '[:lower:]' |
tr -s '[:punct:][:space:]' |
awk '{a[$1]++}END{for (i in a) if (a[i] == 1) {print i}}'
在你的例子中,你不能这样做
cat files | ... | awk '{}' file
^^^^^ ^^^^
您只需指定文件一次。