wc - macOS
Overview
wc (word count) is a command-line utility that counts and displays the number of lines, words, and bytes contained within a given file or input stream. It’s commonly used for text analysis and statistical purposes.
Syntax
wc [options] path/to/file
Options/Flags
-c, --bytes: Print the total number of bytes in the file.-m, --chars: Print the total number of characters (including whitespace) in the file.-l, --lines: Print the total number of lines in the file.-w, --words: Print the total number of words in the file. Default behavior.-L, --max-line-length: Print the length of the longest line in the file.
Examples
Count lines, words, and bytes in a file:
wc my_file.txt
Output:
6 12 100 my_file.txt
Count lines of stdin:
echo "Hello World" | wc -l
Output:
1
Count characters in a file:
wc -m my_file.txt
Common Issues
- Unexpected character count: This can occur when dealing with non-ASCII characters. Use
wc -mfor more accurate character counts. - Incorrect word count: Words are separated by whitespace by default. Use
trto change the separator if needed.
Integration
wc can be combined with other commands for more complex tasks:
- Counting words in a directory:
find . -type f -exec wc -w {} \;
- Finding the longest line in multiple files:
wc -L file1.txt file2.txt file3.txt | sort -nr | head -1
Related Commands
tr: Translate or delete characters.split: Split a file into smaller pieces.sort: Sort lines of text.