WC Command

WC Command Examples

Practical purpose:

Most of the times, when we work with scripting programs,

we might encounter with a requirement of counting the number of lines of a file for writing the logic. We may want to count the lines/words/characters of a command output.

This kind of requirements are best addressed by “wc command”.

 

wc command syntax:

wc file-name
wc file-name1 file-name2

 

Example-1:

Checking the lines,words and characters in a file:

[rreddy@abclearn abclearn_dir1]$ wc  abclearn_lab1.txt
1 7 42 abclearn_lab1.txt

 

From the output,

1st column represents number of line in file

2nd column represents number of words in file

3rd column represents number of characters in file.

 

Example-2:

Counting the words in a file or in a content:

[rreddy@abclearn abclearn_dir1]$ wc -w abclearn_lab1.txt
7 abclearn_lab1.txt

 

Example-3:

Counting the lines in a file or in an output:

[rreddy@abclearn abclearn_dir1]$ wc -l abclearn_lab4.txt
15 abclearn_lab4.txt

 

We can also count number of line in more than one file, also.

[rreddy@abclearn abclearn_dir1]$ wc -l abclearn_lab4.txt abclearn_lab3.txt
15 abclearn_lab4.txt
8 abclearn_lab3.txt
23 total

 

Example-4:

Printing the byte counts of the file:

By using "-c" option with wc command we can print the byte counts of a particular file.

[rreddy@abclearn abclearn_dir1]$ wc -c abclearn_lab4.txt
1816 abclearn_lab4.txt

 

Example-5:

Printing the length of largest line:

By using "-L" option with wc command we can print the length of largest line of a particular file.

[rreddy@abclearn abclearn_dir1]# wc -L  abclearn_lab4.txt
79 abclearn_lab4.txt

 

Example -6:

Printing the new line count or Line count:

By using "-l" option with wc command we can print the new line (/n or enter) count of the particular file.

[rreddy@abclearn abclearn_dir1]$ wc -l abclearn_lab4.txt
39 abclearn_lab4.txt

 

Topics Summary