6 WC Command Examples to Count Number of Lines, Words, Characters in Linux
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.
So, let’s see how we can use the ‘wc‘ command with their few available arguments and examples in this article. We have used the ‘tecmint.txt‘ file for testing the commands. Let’s find out the output of the file using cat command as shown below.
[root@tecmint ~]# cat tecmint.txt Red Hat CentOS Fedora Debian Scientific Linux OpenSuse Ubuntu Xubuntu Linux Mint Pearl Linux Slackware Mandriva
1. A Basic Example of WC Command
The ‘wc‘ command without passing any parameter will display a basic result of ”tecmint.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@tecmint ~]# wc tecmint.txt 12 16 112 tecmint.txt
2. 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@tecmint ~]# wc -l tecmint.txt 12 tecmint.txt
3. Display 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@tecmint ~]# wc -w tecmint.txt 16 tecmint.txt
4. Count Number of Bytes and Characters
When using options ‘-c‘ and ‘-m‘ with ‘wc‘ command will print the total number of bytes and charactersrespectively in a file.
[root@tecmint ~]# wc -c tecmint.txt 112 tecmint.txt
[root@tecmint ~]# wc -m tecmint.txt 112 tecmint.txt
5. 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@tecmint ~]# wc -L tecmint.txt 16 tecmint.txt
6. 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@tecmint ~]# 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/> General help using GNU software: <http://www.gnu.org/gethelp/> For complete documentation, run: info coreutils 'wc invocation'
How to Count Number of Lines in a File in Linux (wc and nl Command Examples)
Linux commands wc and nl will help you to identify the number of words, lines, bytes, etc, in a file. This tutorial explains how to use these two very useful command with various examples.
The basic text file that will be used in examples throughout this article is shown below :
$ cat sort.txt UK Australia Newzealand Brazil America
Linux nl Command Examples
The nl utility in Linux is used to number lines of a file.
Here is the syntax and description from man page :
SYNOPSIS
nl [OPTION]… [FILE]…DESCRIPTION
Write each FILE to standard output, with line numbers added. With no FILE, or when FILE is -, read standard input.
1. A basic example
Here is a basic example that explains how nl command can be used to number lines of a file.
$ cat sort.txt UK Australia Newzealand Brazil America $ nl sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America
So we see that using nl command, all the lines of file sort.txt got numbered.
2. Increment line numbers with any value using -i option
The option -i can be used to override the default increment of 1 in line numbers.
Here is an example where we have used -i to increase the line number increment to 5 :
$ nl -i5 sort.txt 1 UK 6 Australia 11 Newzealand 16 Brazil 21 America
Instead of the default 1,2,3… the line numbers are now displayed in increments of 5 (i.e 1,6,11…)
3. Add string after line numbers using -s option
By default, the nl command adds only line numbers. But, through -s option, any string can be added that can act as a separator between line numbers and the line text.
Here is an example:
$ nl -s. sort.txt 1.UK 2.Australia 3.Newzealand 4.Brazil 5.America
So we see that the character ‘.’ was added after line numbers.
4. Use a different column for line numbers using -w option
Columns for line number display can be changed using -w option.
Here is an example :
$ nl -w1 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -w2 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -w3 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -w4 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -w5 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -w6 sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America
The exhaustive output above gives a good idea as to how the display column for line numbers can be changed.
5. Use STYLE for numbering lines using -b option
Various STYLEs are available for line numbering. From the man page :
STYLE is one of:
- a – number all lines
- t – number only nonempty lines
- n – number no lines
- pBRE – number only lines that contain a match for the basic regular expression, BRE
In the example below, I have used a regular expression ‘pA’ as a STYLE with option -b. This regular expression matches the lines beginning with ‘A’ and so nl command numbers only those lines.
$ nl -bpA sort.txt UK 1 Australia Newzealand Brazil 2 America
So we see that only the lines beginning with ‘A’ were numbered.
6. Use different FORMAT for inserting line numbers using -n options
There are various FORMATs available for inserting line numbers. From the man page :
FORMAT is one of:
- ln – left justified, no leading zeros
- rn – right justified, no leading zeros
- rz – right justified, leading zeros
Here is an example that demonstrated all the above formats:
$ nl -nln sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -nrn sort.txt 1 UK 2 Australia 3 Newzealand 4 Brazil 5 America $ nl -nrz sort.txt 000001 UK 000002 Australia 000003 Newzealand 000004 Brazil 000005 America
Please note that you can also use sed command to count the number of lines in a file.
Linux wc Command Examples
The wc utility in Linux is used print information like number of newlines, words, byte counts of a file.
Here is the syntax and description from man page :
SYNOPSIS
wc [OPTION]… [FILE]…
wc [OPTION]… –files0-from=FDESCRIPTION
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. A word is a non-zero-length sequence of characters delimited by white space.
1. A basic example
Here is a basic example of Linux wc command :
$ cat sort.txt UK Australia Newzealand Brazil America $ wc sort.txt 5 5 41 sort.txt
The three numbers produced in output correspond to number of lines, number of words and number of bytes. These three numbers are followed by name of the file.
2. Display word count through -w option
The word count of a file can be displayed explicitly through -w option.
Here is an example :
$ wc -w sort.txt 5 sort.txt
So we see that number of words were printed followed by the file name.
3. Display length of longest line through -L option
The wc command provides an option -L that can be used to display the length of longest line in the file.
Here is an example :
$ wc -L sort.txt 10 sort.txt
So we see that length of the longest line (‘Newzealand’ in our case) was displayed in the output.
4. Display number of newlines through -l option
The wc command provides an option -l through which number of newlines can be displayed in the output.
Here is an example :
$ wc -l sort.txt 5 sort.txt
So we see that there were 5 newlines in the file sort.txt
5. Display number of bytes through -c option
Total number of bytes in a file can be displayed by using -c option of the wc command.
Here is an example :
$ wc -c sort.txt 41 sort.txt
Unix / Linux: grep Word Count Command
How do I count words using grep command under Linux / Unix like operating systems?
You can pass the -c option to grep command to suppress normal output and display a count of matching lines for each input file. The syntax is follows:
grep -c "word" file grep -c "string" file
In this example, search for a word called ‘var’ and display a count of matching lines:
grep -c 'var' /etc/passwd
Sample outputs:
23
You can pass the -v option to count non-matching lines:
grep -v 'var' /etc/passwd
However, this will not count words. To count exact matched words, enter:
grep -o -w 'word' /path/to/file/ | wc -w
The grep -o command will only display matched words and the wc -c command will display the word counts:
grep -o -w 'foo' bar.txt | wc -w