Linux・シェル4 択難易度 標準

words.txt の単語を出現回数の多い順に表示する正しいパイプラインは?

正答

  1. cat words.txt | sort | uniq -c | sort -rn
  2. sort | cat words.txt | uniq -c | sort -rn
  3. cat words.txt | sort | sort -rn | uniq -c
  4. cat words.txt | uniq -c | sort | sort -rn

解説

uniq は隣り合った同じ行しかまとめないので、先に sort で同じ単語を隣接させる必要がある。次に uniq -c で各単語の件数を先頭に付け、最後に sort -rn で件数を数値として降順に並べる。sort -rn を uniq -c の前に置くと件数が付いていない段階で並べることになり意味がない。-n を忘れて sort -r だけにすると 9 が 10 より大きいと判定される文字列順になる。

参考: www.gnu.org

pipesortuniq

この問題を含む Linux・シェル のテストを制限時間つきで解く

Linux・シェルクイズへ