Command for word count

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.

Support Us

We are thankful for your never ending support.

From Wikipedia, the free encyclopedia

wc

Wc-unix.png

The wc command

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 count
  • wc -l <filename> prints the line count
  • wc -m <filename> prints the character count
  • wc -w <filename> prints the word count
  • wc -L <filename> prints the length of the longest line (GNU extension)

See also[edit]

  • List of Unix commands

References[edit]

  1. ^ wc – Shell and Utilities Reference, The Single UNIX Specification, Version 4 from The Open Group
  2. ^ wc(1) – FreeBSD General Commands Manual
  3. ^ «wc(1) — Linux man page».
  4. ^ MSX-DOS2 Tools User’s Manual by ASCII Corporation
  5. ^ CoreUtils for Windows
  6. ^ Native Win32 ports of some GNU utilities
  7. ^ 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 Manual
  • wc(1) – Plan 9 Programmer’s Manual, Volume 1
  • wc(1) – Inferno General commands Manual
  • The wc Command by The Linux Information Project (LINFO)

wc stands for word count. As the name implies, it is mainly used for counting purpose.

  • It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.
  • By default it displays four-columnar output.
  • First column shows number of lines present in a file specified, second column shows number of words present in the file, third column shows number of characters present in file and fourth column itself is the file name which are given as argument.

Syntax:

wc [OPTION]... [FILE]...

Let us consider two files having name state.txt and capital.txt containing 5 names of the Indian states and capitals respectively.

$ cat state.txt
Andhra Pradesh
Arunachal Pradesh
Assam
Bihar
Chhattisgarh

$ cat capital.txt
Hyderabad
Itanagar
Dispur
Patna
Raipur

Passing only one file name in the argument.

$ wc state.txt
 5  7 58 state.txt
       OR
$ wc capital.txt
 5  5 39 capital.txt

Passing more than one file name in the argument.

$ wc state.txt capital.txt
  5   7  58 state.txt
  5   5  39 capital.txt
 10  12  97 total

Note : When more than file name is specified in argument then command will display four-columnar output for all individual files plus one extra row displaying total number of lines, words and characters of all the files specified in argument, followed by keyword total. Options: 1. -l: This option prints the number of lines present in a file. With this option wc command displays two-columnar output, 1st column shows number of lines present in a file and 2nd itself represent the file name.

With one file name
$ wc -l state.txt
5 state.txt

With more than one file name
$ wc -l state.txt capital.txt
  5 state.txt
  5 capital.txt
 10 total

2. -w: This option prints the number of words present in a file. With this option wc command displays two-columnar output, 1st column shows number of words present in a file and 2nd is the file name.

With one file name
$ wc -w state.txt
7 state.txt

With more than one file name
$ wc -w state.txt capital.txt
  7 state.txt
  5 capital.txt
 12 total

3. -c: This option displays count of bytes present in a file. With this option it display two-columnar output, 1st column shows number of bytes present in a file and 2nd is the file name.

With one file name
$ wc -c state.txt
58 state.txt

With more than one file name
$ wc -c state.txt capital.txt
 58 state.txt
 39 capital.txt
 97 total

4. -m: Using -m option ‘wc’ command displays count of characters from a file.

With one file name
$ wc -m state.txt
56 state.txt

With more than one file name
$ wc -m state.txt capital.txt
 58 state.txt
 39 capital.txt
 97 total

5. -L: 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 Arunachal Pradesh in a file state.txt and Hyderabad in the file capital.txt. But with this option if more than one file name is specified then the last row i.e. the extra row, doesn’t display total but it display the maximum of all values displaying in the first column of individual files. Note: A character is the smallest unit of information that includes space, tab and newline.

With one file name
$ wc -L state.txt
17 state.txt

With more than one file name
$ wc -L state.txt capital.txt
 17 state.txt
 10 capital.txt
 17 total

6. –version: This option is used to display the version of wc which is currently running on your system.

$ wc --version
wc (GNU coreutils) 8.26
Packaged by Cygwin (8.26-1)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later .
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Paul Rubin and David MacKenzie.

Applications of wc Command

1. To count all files and folders present in directory: As we all know ls command in unix is used to display all the files and folders present in the directory, when it is piped with wc command with -l option it display count of all files and folders present in current directory.

$ ls gfg
a.txt 
b.txt  
c.txt  
d.txt  
e.txt  
geeksforgeeks  
India

$ ls gfg | wc -l
7

2. Display number of word count only of a file: We all know that this can be done with wc command having -w option, wc -w file_name, but this command shows two-columnar output one is count of words and other is file name.

$ wc -w  state.txt
7 state.txt

So to display 1st column only, pipe(|) output of wc -w command to cut command with -c option. Or use input redirection(<).

$ wc -w  state.txt | cut -c1
7
      OR
$ wc -w < state.txt
7

?t=89 This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

In this tutorial, we are going to discuss wc command in Linux. Basically, the wc command stands for Word-Count in Linux. ‘wc’ command is used to count the no. of lines, words, and characters in Linux. You just need to mention the file name on which you want to perform count command.

The syntax for the following is :

Wcinput

Here, it would print lines, words, and total characters in the file. ‘<‘ symbol is called input redirection. It simply means that it is taking input from state.txt to execute the wc command. It’s not important to use input redirection symbol. You can simply state the wc and file name to print the total number of lines, words, and characters.

Consider the file on which wc options are performed.

Countcat

Let’s discuss the options used with wc command

  • -w: print the total words
  • -l: prints the newline counts
  • -m: print the total character by character count
  • -c: prints the total characters by byte count
  • -L: prints the length of the longest line
  • –version: to check the version of the command

Print characters by byte count

In the above example, using wc only would print lines, words, and characters as well. But what if, we want to count characters by a byte count? For this, we need to use the ‘-c’ option along with wc as shown in the figure below.

The syntax for the following is:

Wcc

Here, it would print the total number of characters present in the file.

Print number of lines using wc command in Linux

Instead of characters byte count, if you want to print the total number of lines, we will use the ‘-l’ option along with wc command in Linux. Let’s understand from an example.

Wcl

It will print the number of lines present in the file.

Print total characters by character count

Earlier we discussed how to print total character by a byte count. wc command lets you calculate characters by two types; either by byte count or by character count. ‘-c’ is used to print byte count while ‘-m’ is used to printing the character count with the wc command in Linux.

The syntax for the following is:

Wcm

Here, the wc command would print the character count.

Print the length of the longest line

Now, what if you want to print the length of the longest line. For, this we will use the ‘L’ option along with wc command. Folks, don’t get confused in ‘-l’ and ‘-L’ as both are different options. ‘-l’ is used to print the newline counts while ‘-L’ option is used to print the length of the longest line. Let’s understand from an example.

The syntax for the following is:

WcLl

Here, it would print the length of the longest line in the file.

Print word count with the wc command in Linux

Till now, we have discussed how to print the total number of characters and lines. Let’s understand how we will print the total number of words. For this, we will use ‘-w’ to print the total number of words using the wc command in Linux.

The syntax for the following is:

Wcw

Here, it will print the total number of words present in the file.

Store the output in a file

We know how to count the lines, words, and characters. If you want to store the word count result in a file we will use ‘>’ output redirect operator. This simply means it will redirect the output of the wc command to a file. Let’s have a look at the following example given below.

The syntax for the following is:

# wc [filename] > [ filename]
Wcoutpout

It will store the result to file named state1.txt.

Count the Number of Installed Packages

We have so far discussed how to count words, lines, and characters. Let’s have a look at how to count the number of installed packages in Red Hat Linux. For this, we will just combine two commands i.e. rpm and wc. Let’s have a look at the example given below.

The syntax for the following will be:

Wcrpm

rpm stands for Redhat Package Management which is the package manager for Red Hat Linux. ‘-qa’ option is used with rpm to list all the packages installed in the Linux system. Here, the output of the ‘rpm -qa’ will be piped to the wc command to count the number of lines. End result? We get the number of installed packages.

Check the version of wc command in Linux

We use ‘–version’ command to check the version of wc command in Linux.

The syntax for the following is:

Wcversion

It will simply print the version of wc command.

Conclusion

In this tutorial, we’ve gone through how to use the wc command in Linux. If you have any questions, do let us know in the comments.  If you’re interested in learning more about the command, have a look at how to use the man pages in Linux to view the command manual.

I know the command in Unix/Linux systems is «wc» but that doesn’t work in Windows.

asked Mar 20, 2015 at 23:37

Nei Mina's user avatar

«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

Viggy07's user avatar

2

The closest I know of is the PowerShell equivalent Measure-Object.

answered Mar 21, 2015 at 16:01

Alex C. Wolff's user avatar

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

MoonPoint's user avatar

1

Понравилась статья? Поделить с друзьями:
  • Command for microsoft excel
  • Command for bold in word
  • Command buttons on excel
  • Command button in word
  • Comma use after the word but