Tuesday 7 August 2018

WC Command Examples - Count of Lines, Words, Characters - Unix / Linux

WC Command, Linux Certification, Linux Tutorial and Materials, Linux Command

WC command in unix or linux is used to find the number of lines, words and characters in a file. The syntax of wc command is shown below:

wc [options] filenames

You can use the following options with the wc command.

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

Let see how to use the wc command with few examples. Create the following file in your unix or linux operating system.

> cat unix_wc.bat
Oracle Storage
unix distributed system
linux file server
debian server
Oracle backup server

WC Command Examples:


1. Printing count of lines


This is the most commonly used operation to find the number of lines from a file. Run the below command to display the number of lines:

wc -l unix_wc.bat
5 unix_wc.bat

Here in the output, the first field indicates count and second field is the filename

2. Displaying the number of words.


Just use the -w option to find the count of words in a file. This is shown below:

wc -w unix_wc.bat
13 unix_wc.bat

3. Print count of bytes, count of characters from a file


We can use the -c and -m options to find the number of bytes and characters respectively in a file.

> wc -c unix_wc.bat
92 unix_wc.bat
> wc -m unix_wc.bat
92 unix_wc.bat

4. Print the length of longest line


The -L option is used to print the number of characters in the longest line from a file.

wc -L unix_wc.bat
23 unix_wc.bat

WC Command, Linux Certification, Linux Tutorial and Materials, Linux Command

In this example, the second line is the longest line with 23 characters.

5. Print count of lines, words and characters.


If you dont specify any option to the wc command, by default it prints the count of lines, words and characters. This is shown below:

wc unix_wc.bat
 5 13 92 unix_wc.bat

6. Wc help


For any help on the wc command, just run the wc --help on the unix terminal.

Related Posts

0 comments:

Post a Comment