Special File Types   «Prev 

Utilizing the `wc` (Word Count) Command for Counting Lines, Words, and Characters in a File

The `wc` (word count) command in Unix or Unix-like operating systems is a crucial utility for obtaining various statistics about a file. It outputs the number of lines, words, and bytes (characters) in a file. This document meticulously details the use of `wc` command to count the lines, words, and characters in a file.

wc Syntax:

The basic syntax of the `wc` command is as follows:
wc [options] [file...]

Options:

Below are the principal options used with the `wc` command:
  1. `-l`: Count the number of lines in a file.
  2. `-w`: Count the number of words in a file.
  3. `-c` or `-m`: Count the number of bytes or characters in a file.

wc Execution:

Counting Lines:

To count the number of lines in a file, use the `-l` option. For instance:
wc -l filename.txt

This command will return the number of lines in `filename.txt`.

Counting Words:

To count the number of words in a file, use the `-w` option:
wc -w filename.txt

This command will return the number of words in `filename.txt`.

Counting Characters:

To count the number of bytes (or characters) in a file, use the `-c` or `-m` option:
wc -c filename.txt
or
wc -m filename.txt

Either command will return the number of bytes or characters in `filename.txt`.

Unix Examples

  1. Count lines, words, and characters in a file:
    wc filename.txt
    
  2. Count lines only:
    wc -l filename.txt
    
  3. Count words only:
    wc -w filename.txt
    
  4. Count characters only:
    wc -c filename.txt
    
In Unix or Unix-like operating systems, the `wc` command is a powerful tool for obtaining line, word, and character counts from a file. Understanding and effectively utilizing this command enhances the efficiency and productivity of tasks related to text processing and analysis.
The 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 http-protocol.jsp

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 input.

Common Options

  1. -c, --bytes: Print byte count only.
    [root@ip-171-32-8-155 module1]# wc --bytes setting-up-environment.jsp
    7705 setting-up-environment.jsp
    
  2. -l, --lines: Print line count only.
    [root@ip-171-32-8-155 module1]# wc --lines setting-up-environment.jsp
    112 setting-up-environment.jsp
    
  3. -m, --chars: Print character count only. This will be different than -c in a multibyte character environment.
    [root@ip-171-32-8-155 module1]# wc --chars setting-up-environment.jsp
    7705 setting-up-environment.jsp
    
  4. -w, --words: Print word count only.
    [root@ip-171-32-8-155 module1]# wc --words setting-up-environment.jsp
    1039 setting-up-environment.jsp
    

Solaris Option

-C Same as -m.

Linux Option

-L, --max-line-length
Print length of longest line.
Examples
1. Count the number of users logged in:
who | wc -l

2.Count the words in three essay files:
wc -w essay.[123]

3. Count lines in file named by $file (do not display filename):
wc -l < $file

would output the number of lines in the file named file1.
The head and 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.

For example:
head -15 http-protocol.jsp

would display the first 15 lines of the file named http-protocol.jsp and
tail file2

would display the last 10 lines of the file named file2.
Unix System Administration