46
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

文字列の出現回数を調べるawk

Last updated at Posted at 2014-02-28

SQLでいうところのGROUP BYのようなことをしたい場合。awkで出来たりします。

調べたいファイル
$ cat fruits.list
apple
orange
orange
apple
melon
結果
$ cat fruits.list | awk '{count[$0]++}END{for(i in count)print count[i], i}'
2 apple
1 melon
2 orange

実は unique -c使ったほうが簡単です。

uniq
$ cat fruits.list | sort | uniq -c
      2 apple
      1 melon
      2 orange

fruits.listがめちゃくちゃデカい場合とかにはsortのコストがバカにならないのでawkの方がいいですね

データの件数が多い場合はawkに利点があるようです!

46
40
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
40

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?