wc – command

The wc (word count) command in Unix/Linux operating systems is used to find out number of newline count, word count, byte and characters count in a files specified by the file arguments. The syntax of wc command as shown below.

# wc [options] filenames

The following are the options and usage provided by the command:

wc -l : Prints the number of lines in a file.
wc -w : prints the number of words in a file.
wc -c : Displays the count of bytes in a file.
wc -m : prints the count of characters from a file.
wc -L : prints only the length of the longest line in a file.

I have used the ‘test.txt‘ file for testing the commands. Let’s find out the output of the file using cat command as shown below.

[root@[hostname] ~]# cat test.txt

Red Hat
CentOS
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Pearl Linux
Slackware
Mandriva

A basic example of WC command:
The ‘wc‘ command without passing any parameter will display a basic result of ”test.txt‘ file. The three numbers shown below are 12 (number of lines), 16 (number of words) and 112 (number of bytes) of the file.

[root@[hostname] ~]# wc test.txt

12 16 112 test.txt

Count number of lines:
To count number of newlines in a file use the option ‘-l‘, which prints the number of lines from a given file. Say, the following command will display the count of newlines in a file. In the output the first filed assigned as count and second field is the name of file.

[root@[hostname] ~]# wc -l test.txt

12 test.txt

Display the number of words:
Using ‘-w‘ argument with ‘wc‘ command prints the number of words in a file. Type the following command to count the words in a file.

[root@[hostname] ~]# wc -w test.txt

16 test.txt

Count number of bytes and characters:
When using options ‘-c‘ and ‘-m‘ with ‘wc‘ command will print the total number of bytes and characters respectively in a file.

[root@[hostname] ~]# wc -c test.txt

112 test.txt

[root@test ~]# wc -m test.txt

112 test.txt

Display length of longest line:
The ‘wc‘ command allow an argument ‘-L‘, it can be used to print out the length of longest (number of characters) line in a file. So, we have the longest character line (‘Scientific Linux‘) in a file.

[root@[hostname] ~]# wc -L test.txt

16 test.txt

Check more wc options:
For more information and help on the wc command, simple run the ‘wc –help‘ or ‘man wc‘ from the command line.

[root@[hostname] ~]# wc –help

Usage: wc [OPTION]… [FILE]…
or: wc [OPTION]… –files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. With no FILE, or when FILE is -,
read standard input.
-c, –bytes print the byte counts
-m, –chars print the character counts
-l, –lines print the newline counts
-L, –max-line-length print the length of the longest line
-w, –words print the word counts
–help display this help and exit
–version output version information and exit

Report wc bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/&gt;
General help using GNU software: <http://www.gnu.org/gethelp/&gt;
For complete documentation, run: info coreutils ‘wc invocation’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s