Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

テキストデータの行数や文字数をカウントしたい場合はwcコマンドを使う

wcは"word, line, character, and byte count"との説明通り、文字数や行数を数えるコマンドです。
原則オプションと組み合わせて使います。

-m The number of characters in each input file is written to the standard output. If the current locale does not support multibyte characters, this is equivalent to the -c option. This will cancel out any prior usage of the -c option.

$ printf '日本' | wc -m
       2
$ printf 'japan' | wc -m
       5

-c The number of bytes in each input file is written to the standard output. This will cancel out any prior usage of the -m option.

$ printf '日本' | wc -c
       6
$ printf 'japan' | wc -c
       5

l

-l The number of lines in each input file is written to the standard output.

$ printf '日本' | wc -l
       0

$ printf 'japan' | wc -l
       0

複数ファイルをまとめてカウントしたい場合はxargsと組み合わせて使います。

$ grep rb | xargs wc -l