Skip to content Skip to sidebar Skip to footer

Write a Program That Will Read in a Line of Text and Output the Number of Words

What is the wc command in Linux?

The wc command displays statistical information about a file such as the number of lines, words, characters.

Trivia: wc stands for word count.

The syntax for the wc command is:

            wc [options] [files]          
wc command syntax
wc command syntax

wc command has the following options:

  • –l : Prints the number of lines only
  • –w : Prints the number of words only
  • -c : Prints the number of bytes but
  • –m : Prints the count of characters (different than the number of bytes for not-text files)
  • –L : Prints the length of the longest line in the file
  • —files0-from=F : Get the file names from file F (file names must be separated past the Zip character)

6 practical examples of wc command in Linux

I am going to use agatha.txt and sherlock.txt files in this example. You may download these files and do the wc command examples forth with this article.

If you use wc command with just the input file proper noun(s), without whatsoever options, information technology will show you the count of the lines, words and bytes at the same time.

            wc agatha.txt 20  lxxx 457 agatha.txt          

In the above output:

  • xx is the number of lines
  • 80 is the number of words
  • 457 is the number of bytes

Now that you are aware of the wc control options, permit'due south encounter some examples of wc control.

1. Count the number of lines in a file

If you just want to know the number of lines in a text file, y'all can use the wc control with pick 'l'. Basically, it counts the number of newlines in the file.

            wc -l agatha.txt 20 agatha.txt          

2. Count the number of words in a file

If you just want to know the number of words in a text file, yous can apply the wc control with option 'w'. It will display the number of whitespace-delimited words.

            wc -w agatha.txt 80 agatha.txt          

3. Count the number of bytes and characters in a file

If it's a regular text file, the number of bytes and characters should be the same. But it will differ for the not-text files.

To display the number of bytes in a file, use wc command with option 'c':

            wc -c agatha.txt 457 agatha.txt          

To brandish the number of characters in a file, utilize wc command with option 'm':

            wc -m agatha.txt 457 agatha.txt          

I know y'all must be thinking that choice 'c' is more suited for the task of counting characters but Unix/Linux commands have always been foreign.

How to Count Number of Files in Directory in Linux [Quick Tip]

Hither are several ways to count the number of files in a given directory in Linux.

4. Display length of the longest line of a file

The 'L' option of wc command displays the length (number of characters) of the longest line of a file.

            wc -L agatha.txt 31 agatha.txt          

5. Display number of lines, words, characters for multiple files

You can employ more than ane file with wc command. Information technology will brandish the output for each of the files one by one forth with the total count in all the files.

For example, if I want to brandish the number of lines of ii files, it would be like this:

            wc -l agatha.txt sherlock.txt twenty agatha.txt 12 sherlock.txt 32 total          

6. Use wc with other commands using pipes

What you saw then far was the straightforward wc command option examples. You can further utilize wc with the output of other commands using the wonderful pipes.

For example, you tin can redirect the output of ls control to wc and thus you can count the full number of files and sub-directories in the given given directory.

            ls | wc -l          

The possibilities are endless. You just need to use your little gray cells to utilize wc command in diverse situations.

Bonus Tip: Remove the filename from wc command output

Yous might have noticed that wc command output consists of the file names. If just want to get the number without the filename, you may use information technology with the cut command and go rid of the filename from the output.

            wc -fifty agatha.txt | cutting -d ' ' -f 1          

You can likewise get rid of the filename by using the wc command in this fashion:

            wc -fifty < agatha.txt          

I hope y'all liked this tutorial on using wc control in Linux. You can also learn how to count the number of files in a directory in Linux past combining wc command and ls command.

If you accept questions or suggestions, delight leave a annotate below. If you liked the article, please share information technology on social media and help us attain more people.

defilippoalmou1979.blogspot.com

Source: https://linuxhandbook.com/wc-command/

Post a Comment for "Write a Program That Will Read in a Line of Text and Output the Number of Words"