wc (short for word count) is a command line tool in Unix/Linux operating systems, which is used to find out the number of newline count, word count, byte and character count in the files specified by the File arguments to the standard output and hold a total count for all named files.
When you define the File parameter, the wc command prints the file names as well as the requested counts. If you do not define a file name for the File parameter, it prints only the total count to the standard output.
In this article, we will discuss how to use the wc command to calculate a file’s newlines, words, characters, or byte count with practical examples.
wc Command Syntax
The syntax of the wc command is shown below.
# wc [options] filenames
The followings are the options and usage provided by the wc 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.
Let’s see how we can use the ‘wc‘ command with the 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 tecmint.txt file using the cat command as shown below.
$ cat tecmint.txt Red Hat CentOS AlmaLinux Rocky Linux Fedora Debian Scientific Linux OpenSuse Ubuntu Xubuntu Linux Mint Deepin Linux Slackware Mandriva
1. A Basic Example of WC Command
The ‘wc‘ command without passing any parameter will display a basic result of the ‘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.
$ wc tecmint.txt 12 16 112 tecmint.txt
2. Count Number of Lines in a File
Count the number of newlines in a file using 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 field is assigned as count and the second field is the name of the file.
$ wc -l tecmint.txt 12 tecmint.txt
3. Count Number of Words in a File
The -w
argument with the wc command prints the number of words in a file. Type the following command to count the words in a file.
$ wc -w tecmint.txt 16 tecmint.txt
4. Count Number of Characters in a File
When using option -m
with the wc command will print the total number of characters in a file.
$ wc -m tecmint.txt 112 tecmint.txt
5. Count Number of Bytes in a File
When using option -c
will print the number of bytes of a file.
$ wc -c tecmint.txt 112 tecmint.txt
6. Display Length of Longest Line in File
The ‘wc‘ command allows an argument ‘-L
‘, it can be used to print out the length of the longest (number of characters) line in a file.
So, we have the longest character line (‘Scientific Linux‘) in a file.
$ wc -L tecmint.txt 16 tecmint.txt
7. Check wc Command Options
For more information and help on the wc command, simply run the ‘wc --help
‘ or ‘man wc
‘ from the command line.
$ wc --help OR $ man wc
wc Command Usage
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. A word is a non-zero-length sequence of characters delimited by white space. With no FILE, or when FILE is -, read standard input. The options below may be used to select which counts are printed, always in the following order: newline, word, character, byte, maximum line length. -c, --bytes print the byte counts -m, --chars print the character counts -l, --lines print the newline counts --files0-from=F read input from the files specified by NUL-terminated names in file F; If F is - then read names from standard input -L, --max-line-length print the maximum display width -w, --words print the word counts --help display this help and exit --version output version information and exit GNU coreutils online help: <https://www.gnu.org/software/coreutils/> Full documentation at: <https://www.gnu.org/software/coreutils/wc> or available locally via: info '(coreutils) wc invocation'
In this article, you’ve learned about the wc command, which is a simple command-line utility to count the number of lines, words, characters, and byes in text files. There are lots of such other Linux commands, you should learn and master your command-line skills.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
From Wikipedia, the free encyclopedia
The |
|
Original author(s) | Joe Ossanna (AT&T Bell Laboratories) |
---|---|
Developer(s) | Various open-source and commercial developers |
Initial release | November 3, 1971; 51 years ago |
Written in | C |
Operating system | Unix, Unix-like, V, Plan 9, Inferno, MSX-DOS, IBM i |
Platform | Cross-platform |
Type | Command |
License | Plan 9: MIT License |
wc
(short for word count) is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.
Example[edit]
Sample execution of wc:
$ wc foo bar 40 149 947 foo 2294 16638 97724 bar 2334 16787 98671 total
The first column is the count of newlines, meaning that the text file foo
has 40 newlines while bar
has 2294 newlines- resulting in a total of 2334 newlines. The second column indicates the number of words in each text file showing that there are 149 words in foo
and 16638 words in bar
– giving a total of 16787 words. The last column indicates the number of characters in each text file, meaning that the file foo
has 947 characters while bar
has 97724 characters – 98671 characters all in all.
Newer versions of wc
can differentiate between byte and character count. This difference arises with Unicode which includes multi-byte characters. The desired behaviour is selected with the -c
or -m
options.
Through a pipeline, it can also be used to preview the output size of a command with a potentially large output, without it printing the text into the console:
$ grep -r "example" |wc 1071 23337 101349
History[edit]
wc
is part of the X/Open Portability Guide since issue 2 of 1987. It was inherited into the first version of POSIX.1 and the Single Unix Specification.[1] It appeared in Version 1 Unix.[2]
GNU wc
used to be part of the GNU textutils package; it is now part of GNU coreutils. The version of wc
bundled in GNU coreutils was written by Paul Rubin and David MacKenzie.[3]
A wc
command is also part of ASCII’s MSX-DOS2 Tools for MSX-DOS version 2.[4]
The command is available as a separate package for Microsoft Windows as part of the GnuWin32 project[5] and the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[6]
The wc command has also been ported to the IBM i operating system.[7]
Usage[edit]
wc -c <filename>
prints the byte countwc -l <filename>
prints the line countwc -m <filename>
prints the character countwc -w <filename>
prints the word countwc -L <filename>
prints the length of the longest line (GNU extension)
See also[edit]
- List of Unix commands
References[edit]
- ^
wc
– Shell and Utilities Reference, The Single UNIX Specification, Version 4 from The Open Group - ^
wc(1)
– FreeBSD General Commands Manual - ^ «wc(1) — Linux man page».
- ^ MSX-DOS2 Tools User’s Manual by ASCII Corporation
- ^ CoreUtils for Windows
- ^ Native Win32 ports of some GNU utilities
- ^ IBM. «IBM System i Version 7.2 Programming Qshell» (PDF). Retrieved 2020-09-05.
{{cite web}}
: CS1 maint: url-status (link)
External links[edit]
- wc(1) — Original Unix First Edition manual page for wc.
wc(1)
– Linux User Commands Manualwc(1)
– Plan 9 Programmer’s Manual, Volume 1wc(1)
– Inferno General commands Manual- The
wc
Command by The Linux Information Project (LINFO)
Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action in a shell script.
There may be some scenarios where one needs to keep track of the number of lines and number of words in a particular file. In that scenario, any of the following methods can be used to count the number of the lines and the words in a particular file in Linux. Let’s take some examples for better understanding:
Example: Consider this file(demo.txt) with the following content:
This is first line. This is second line. This is third line.
Output:
Number of line = 3 Number of words = 12
Let us have a look at all the methods to count the number of lines and words and how they can be used in a shell script.
Method 1: Using WC command
wc stands for word counts. Using wc command the number of words, number of lines, number of white spaces, etc can be determined.
Syntax-
wc [option] [input-file]
Approach:
- Create a variable to store the file path.
- Use wc –lines command to count the number of lines.
- Use wc –word command to count the number of words.
- Print the both number of lines and the number of words using the echo command.
Input file: cat demo.txt
This is first line This is second line This is third line
cat command is used to show the content of the file.
Script:
#!/usr/bin/bash # path to the file file_path="/home/amninder/Desktop/demo.txt" # using wc command to count number of lines number_of_lines=`wc --lines < $file_path` # using wc command to count number of words number_of_words=`wc --word < $file_path` # Displaying number of lines and number of words echo "Number of lines: $number_of_lines" echo "Number of words: $number_of_words"
Output:
Using wc command
Explanation:
- The first line tells the system that bash will be used as an interpreter.
- The wc command is used to find out the number of lines and number of words.
- A variable is created to hold the file path.
- After that, wc command is used with –lines argument to count the number of lines, and similarly, wc command with –words argument is used to count the number of words in the file.
- In the end, the number of words and the number of lines is displayed using the echo command.
NOTE: Lines starting with the “#” symbol are called comments and ignored by the interpreter except for the first line.
Method 2: Using awk command
awk is a scripting language mainly used for text preprocessing and text manipulation. Using awk, we can do pattern search, find and replace, count words, count lines, count special symbols, count white spaces, etc.
Syntax:
awk {action-to-be-performed} [input-file]
Approach 1:
- Create a variable to store the file path.
- Initialize a counter variable to count the number of lines.
- After every line increment the counter variable to count the number of lines.
- Display the number of lines using the print command.
- Initialize another counter variable to count the number of words.
- Use white space as a Record Separator and increment the counter variable to count the number of words separated by space.
- After that, display the number of words using the print command.
Script:
#!/usr/bin/bash # path to the file file_path="/home/amninder/Desktop/demo.txt" # Method 1 echo "Using method 1" # using awk command to count number of lines awk 'BEGIN{c1=0} //{c1++} END{print "Number of lines: ",c1}' $file_path #using awk command to count number of words awk 'BEGIN{c=0} //{c++} END{print "Number of words: ",c}' RS="[[:space:]]" $file_path
Output:
Using method 1
Explanation:
- In the first line, a variable file_path is created to hold the path of the text file.
- The awk command statement can be divided into the following parts.
- BEGIN{c=0} will initialize a count variable called. //{c++} will increment the count variable c by 1, whenever it encountered a new line.
- END{print “Number of lines: “, c} will print the number of lines.
- Similarly, the number of words are counted by separating each word by space using RS=”[[:space:]]. Here, RS is a Record Separator, and space is used as a separator in this example.
Note: Lines starting with the “#” symbol are called comments and ignored by the interpreter except for the first line.
Approach 2:
- Create a variable to store the file path.
- Use a special NR variable to find out the number of lines. NR means the number of records, and it holds the number of processed records.
- Use NF(Number of fields in the current record) to find out the number of words in each line.
- Then use a while loop to traverse through all the lines and sum up the NF from each line.
- Display the number of lines.
Example for NF: Let’s file be-
First line is on top Second line is on second last position
NF means the number of fields in the current record i.e. number of words in the current line.
command: awk '{print NF}' demo.txt Output: 5 7
Here, 5 represents that there are 5 words in the first line, and 7 means there are 7 words in the second line.
Script:
#!/usr/bin/bash # path to the file file_path="/home/amninder/Desktop/demo.txt" # Method 2 echo "Using method 2" # using NR to count number of lines awk 'END{print "Number of lines:",NR}' $file_path #using awk command to count number of words awk '{i=0; count=0; while (i<NR) { count+=NF; i++;}} END {print "Number of words are: " count}' $file_path
Output:
Using method 2
Explanation:
- In the first line, a variable file_path is created to hold the path of the text file.
- Then, the number of lines is printed using ‘END{print “Number of lines:”,NR}’.
- Here, END represents that we are interested in the last values of the NR variable as NR variable holds the count of the processed records.
- To count the number of lines, a while loop is used till the number of processed records.
- Adding the value of the NF i.e. count of words in each line.
- In the end, the number of words is printed that is stored in the count variable.
I know the command in Unix/Linux systems is «wc» but that doesn’t work in Windows.
asked Mar 20, 2015 at 23:37
«Find» will be able to do the task similar to word count as RRM told.
Eg.
Query user | find /v /c «»
/v – Show any lines that don’t contain the string of words you specified.
/c — Count the number of lines that matches the word in fine.
Query user | find /i «active» /c
/i — Ignore case
/c — Count the number of lines that matches the word in fine.
answered Mar 5, 2018 at 18:56
2
The closest I know of is the PowerShell equivalent Measure-Object.
answered Mar 21, 2015 at 16:01
wc is part of the GNU Core Utils for Windows. I often install GnuWin32 utilities on Microsoft Windows systems to supply equivalents to the commands I use regularly on Linux and OS X systems.
answered Mar 21, 2015 at 2:20
1
In UNIX-like operating systems, we predominantly use the shell to execute various Linux commands to accomplish common tasks. The WC command is one of the prevalent commands utilized to retrieve information related to the contents of a text file.
In this tutorial, we learn about wc command and its options with examples.
The WC command is used to get the number of lines, word count, characters count, or byte counts in the specified file, multiple files, or standard input. The command name WC is shortened for word count. wc comes most useful in bash programming to find file content stats.
The WC command presents its output in a table format which consists of four columns. Each column shows the number of lines, words, bytes, and the name of the input object specified as a file argument. The line count includes empty lines as well.
Syntax:
wc [Options]... [File]...
The options and file name arguments are optional to the WC command. When specified, the command accepts multiple options and files as arguments. If no File argument is specified, the WC command reads from the standard input.
When you use the WC command without any options, by default, it will print four columns starting from the count of lines, words, characters, and finally the file names specified in the File arguments list. If the command is reading from standard input, the fourth column will be dropped.
wc options
Useful options of wc command:
Options | Description |
---|---|
-l | It prints the number of lines in the given file or standard input. |
-w | The word counts of the specified file or files will be printed. |
-m | The number of bytes contained in the specified file or files will be printed. |
-c | The byte count of a given file/s will be the output. |
-L | Find the longest lines present in the input and displays their length. |
wc options Examples
The WC command accepts five major optional arguments along with one or more than one file name. When multiple options specified, the output will be in the order of count of lines, words, characters, bytes, and maximum line length.
-l option
We can use the -l option to count only the number of lines of a given file as follows.
wc -l continents.txt
Let’s specify two files and count the number of lines in each file.
wc -l continents.txt oceans.txt
-w option
The -w option can be used to get only the number of words present in a file as shown in the following command.
wc -w continents.txt
Similarly, more than one file can be specified in the command to count the number of words in each file separately.
-c option
When you need to count only the number of bytes in a given file, the -c option can be used.
wc -c continents.txt
Let’s execute the same command by specifying multiple files as follows.
wc -c continents.txt oceans.txt
-m option
The -m argument can be specified to retrieve only the character count in a single file as shown in the following command.
wc -m continents.txt
For multiple files, the command would look as follows.
wc -m continents.txt oceans.txt
-L option
The -L option is used to find the length of the longest line contained in a given file. In this example, the continents.txt contains the line «Main Continents in the Earth.» which contains 29 characters. Hence it is the longest line present in this particular file.
wc -L continents.txt
It is possible to get the maximum length of the longest line contained in multiple files using the -L option. The maximum line length will be displayed separately for each file specified. In this case, the extra line total won’t display, instead, the maximum line length among all the specified files will be displayed.
wc -L continents.txt oceans.txt
Some Practical Examples
The wc command can be piped with other commands to accomplish regular tasks listed in the following.
1. Count the number of times a word appears in a file
We can count the number of times a given word appeared in a file as follows.
cat continents.txt | grep -o 'America' | wc -l
First, we get the contents of the specified file using the cat command and pipe the output to the grep command in search of the specified word. Finally, this output is piped to the wc command with the -l option which counts only the number of words.
Since the word ‘America’ appeared in two places of the continents.txt file, the output is 2.
2. Count the number of files and directories in current directory
In some cases, we need to count the number of files and directories that reside in the current directory. It is quite straightforward to use the wc command along with the ls command.
ls -1 | wc -l
OR
you can use the following command to count only the number of files residing in the current directory. In this example, we first find the files by type and pipe the results into the wc command.
find . -type f | wc -l
3. Count the total number of users
The wc command is also used to count the number of users in databases like group and passwd where all the user information is stored.
getent passwd | wc -l
Conclusion
To conclude, the command line command WC which stands for word count used to count the number of lines, words, characters, bytes, and the number of characters contained in the longest line in a given file or multiple files.
Overall, the wc command can be used to count different things in text files and the functionality can be extended by piping with other shell commands.
Thanks for reading, please leave your feedback and suggestions in the comment below.
About The Author
Nimesha Jinarajadasa
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called «03 tiers(DB, M-Tier, and Client)». Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.