Word Count Command in Unix
wc, head, and tail Commands used with Special File Types
wc command gives you a line, word, or character count of a file. You can use the
–l option for a line count, the
–c option for a character count, and the
–w option for a word count. For example:
wc –l file1
wc wc [options] [files]
Word count. Print a character, word, and line count for files. If multiple files, print totals as well. If no files are given, read standard
- -c, --bytes: Print byte count only.
- -l, --lines: Print line count only.
- -m, --chars: Print character count only. This will be different than -c in a multibyte character environment.
- -w, --words: Print word count only.
-C Same as -m.
Print length of longest line.
1. Count the number of users logged in:
who | wc -l
2.Count the words in three essay files:
wc -w essay.
3. Count lines in file named by $file (do not display filename):
wc -l < $file
Unix System Administration
would output the number of lines in the file named file1.
tail commands allow you to display the first (
head) or last (
tail) part of a specified file. The default amount to be displayed is 10 lines. These commands read from standard input if no files are given or when a filename of
- is encountered.
head –15 file1
would display the first 15 lines of the file named file1 and
would display the last 10 lines of the file named file2.