LoginSignup
4
3

More than 5 years have passed since last update.

awkで特定の文字をカウントする

Posted at

文字列中に含まれる特定の文字数を数える awk プログラムによると、split()を使って特定の文字の数を数えることができるようなので、試してみました。

テスト用ファイルを作成する

テスト用に、タブが4つ含まれたファイルを作成します。

タブが4つ含まれたファイルを作成する
miminashi@mmns2016:02$ printf 'aaa\tbbb\tccc\n' > hoge.txt 
miminashi@mmns2016:02$ printf 'aaa\tbbb\tccc\n' >> hoge.txt 
miminashi@mmns2016:02$ cat hoge.txt 
aaa bbb ccc
aaa bbb ccc

文字をカウントする

文字をカウントする
$ cat hoge.txt | awk '{count += (split($0, a, "\t") - 1)} END{print count}'
4
4
3
0

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
4
3