ワード カウント (wc) コマンドは、テキスト ファイル内の行数、単語数、および文字数をカウントするために使用されます。複数のファイルが指定されている場合、コマンドは各ファイルのカウントとすべてのファイルの合計カウントを表示します。
構文
wc コマンドの構文は次のとおりです。
# wc [options] {file names} wc コマンド オプション
wc コマンドには、出力の性質を指定できるさまざまなオプションがあります。
| オプション | 慣れている |
|---|---|
| -c | バイト数を表示します。 |
| -m | 文字数を表示します。 |
| -l | 改行数を表示します。 |
| -w | 単語数を表示します。 |
wc コマンドの実行中に以下のエラーが発生した場合:
wc: command not found
選択したディストリビューションに従って、以下のパッケージをインストールしてみてください:
| 分布 | コマンド |
|---|---|
| OS X | brew install coreutils |
| Debian | apt-get install coreutils |
| Ubuntu | apt-get install coreutils |
| アルパイン | apk add coreutils |
| Arch Linux | pacman -S coreutils |
| カリ Linux | apt-get install coreutils |
| CentOS | yum install coreutils |
| Fedora | dnf install coreutils |
| Raspbian | apt-get install coreutils |
wc コマンドの例
1. ファイルの行数を確認するには:
# wc -l test.txt
2. バイト数をカウントするには:
# wc -c test.txt
3. 文字数を取得するには:
# wc -m test.txt
4. 単語数を取得するには:
# wc -w test.txt
5. 最長の行の長さを取得するには:
# wc -L test.txt
6. バージョンを取得するには:
# wc --version
7. `stdin` からすべての行、単語、バイトを数えます:
# find . | wc
8. 最長の行の長さを文字数で数えます:
# wc --max-line-length path/to/file