Let’s discuss about the wc command line tool today.
wc, which stands for word count, prints the count of lines, words and bytes for a file.
Here’s the basic syntax for wc:
wc [OPTION]… [FILE]…
By the way, wc can be used in command line pipelines as well. We’ll get to that toward the end of this post.
Let’s start by trying a few options that come with wc.
Before we do that, we’ll create a text file to use in our examples.
The text file we created is called testing-wc.txt.
$ cat > testing-wc.txt
Ingrid Bergman
Humphrey Bogart
Lauren Bacall
Cary Grant
Gregory Peck
Audrey Hepburn
Ava Gardner
Brad Pitt
Angelina Jolie
Julie Andrews
Shahrukh Khan
Hema Malini
Woody Allen
Christoph Waltz
Gérard Depardieu
1. Basic WC Command
The plain wc command without any options will provide the count for words, lines and characters.
$ wc testing-wc.txt 15 30 207 testing-wc.txt
2. Display Word Count
If you’re looking to print just the word count, use the -w option.
$ wc -w testing-wc.txt 30 testing-wc.txt
3. Display Line Count
The -l option prints out the line count.
$ wc -l testing-wc.txt 15 testing-wc.txt
4. Display Character Count
With -m option, you can display the character count.
$ wc -m testing-wc.txt 206 testing-wc.txt
5. Display Byte Count
Using the -c option gets you the byte count of the file.
$ wc -c testing-wc.txt 207 testing-wc.txt
6.Display Length of Longest Line
$ wc -L testing-wc.txt 16 testing-wc.txt
7. Using WC in Pipeline
Let’s consider an example where we use wc in a command pipeline.
$ cat /etc/passwd | wc -l 36
Here’s another example of how we can use wc in a command pipeline:
$ ls -1 | wc -l 14
8. Get WC Help
You can always get help with wc by using the –help option.
$ 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. A word is a non-zero-length sequence of characters delimited by white space. 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 length of the longest line -w, --words print the word counts --help display this help and exit --version output version information and exit
Aren’t you glad that you’ve added one more command line tool to your arsenal by learning the use of wc.
Why don’t you take wc for a test drive on your Linux computer now.
Sorry, the comment form is closed at this time.