| by Arround The Web | No comments

How to Use the WC Command in Bash

To count the overall number of lines, syllables, and letters in a text document, use the “wc” Bash function, which means “word count”. It is presented with four columns in the output. The first of which displays the number of lines. The second column lists the words in the file. The third column lists the characters. And the final column lists the file name. This “wc” command gives us access to several flags including “-c”, “-w”, and others which we will cover in this article.

Using the “WC” Command in Ubuntu 20.04’s Bash

For each input file, the “wc” function or “word count” outputs the number of new rows, words, and bytes. In general, you may use the “wc” command with a file to get an information on how many lines, characters, or words are in the file. Let’s examine the various instances when we might need this. We must first move the directory to the desktop using the following command because the text file with the “datafile.txt” name is saved on the desktop:

Linux@linux:~$ cd Desktop/

Now, we use the “wc” command with the “datafile.txt” filename to find out how many lines, words, and characters are in the file. After typing the command, we press the enter key on the keyboard to get the outcome.

Linux@linux:~/Desktop$ wc datafile.txt

We show the following “datafile.txt” so you can quickly grasp the output:

Following the execution of this function, this kind of output is returned. The first result here is “3” which indicates that there are three lines in this file. The second output is “10” which indicates that there are 10 words in a line. The third output displays the file’s character count. And the last output displays the file name, “datafile.txt”.

The file’s line count is changed in the following section where we also evaluate the output that it produces after adding more lines. There are now “7” lines, as shown in the following image. As the number of lines increases, the words and characters also increase.

Because we increased the number of lines in the same file, we now use the same command with the same file name which is “datafile.txt”.

Linux@linux:~/Desktop$ wc datafile.txt

To check the output, press the enter key. You can now see in the output on how the number of lines, words, and characters are altered. The line count increases to 7, the word count increases to 60, and the character count increases to 349.

Using the “WC” Command’s Options

The “wc” command gives us several possibilities. This “wc” assists us in finding that using the various options if we simply want to find the number of lines, the bytes that are contained in the file, the largest line which is present in the file, or if we only want to count the multiple text files.

Printing the Number of Bytes Using the “-c” Option

Here, we use the “-c” flag with the “wc” command and the “datafile.txt” file name to determine the number of bytes or characters. Instead of using “-c”, we can alternatively use the “-bytes” with the “wc” command to display the total number of bytes that are present in a text file.

Linux@linux:~/Desktop$ wc -c datafile.txt

When we press the enter key, the number of bytes in the file is shown in the output. The total number of characters in the file is “349”. As seen in the following image, it also prints the file name:

Using the “-l” Option to Print the Line Count

In this section, we use the “wc” command, the “-l” flag, and the text file’s name to output the file’s line count.

Linux@linux:~/Desktop$ wc -l datafile.txt

After running this command, the “datafile.txt” displays that there are “7” lines in the file.

Printing the Word Count with the “-w” Option

Using the “-w” option and the “wc” command, we display the total number of words in the “datafile.txt” in this section. The command is listed in the following. When you execute it using the enter key, the result appears on the screen:

Linux@linux:~/Desktop$ wc -w datafile.txt

The number “60” in the result indicates that the “datafile.txt” contains “60” total words.

Displaying the Characters of the Text File’s Longest Line

The uppercase (-L) flag is used in this section to output the number of characters of the file’s longest line. To begin with, open the file and determine which line is the longest. The following graphic shows the file being used. As you can see in the following figure, there are a total of “7” lines, with line “6” having the most text. We can know from this file that the “6” line is the longest by looking at it. But we can print this information on the output using the “wc” with “-L” command.

Linux@linux:~/Desktop$ wc -L datafile.txt

Because the “6” line is the longest, this command successfully outputs the total number of characters of the “6” line which is “115” in total.

Counting from the Multiple Files

In this part, we create one more text file in this section with the name “file2.txt” now that we have two text files on the desktop, as shown in the following. Then, we tally the rows, words, and letters in both text files using the “wc” program.

Let’s put this command into practice. We use the “wc” command before writing the names of the first and second files, the “datafile.txt” and “file2.txt”, respectively. The output of this command is displayed in three lines. The first line shows the total number of lines, words, and characters of the first file. The second line shows the total number of words, characters, and lines of the second file. Additionally, the third line adds up the words, characters, and lines of both files.

Linux@linux:~/Desktop$ wc datafile.txt file2.txt

When this command is executed, it prints out the information about the files. The first line shows that the “datafile.txt” file has a total of “7” lines, “70” words, and “494” characters. While the second file has a total of “3” lines, “3” words, and “13” characters. The last line shows the sum of both files, making the total number of lines, words, and characters after combining both files as  “10”, “73” and “507”.

 

Conclusion

In this article, we discussed how to use the “wc” Bash function to acquire the total amount of line, words, and characters in a file using the Ubuntu 20.04 version. We utilized the various “wc” command flags such as “-c,” “-L,” and “-w”. In the final section, we printed the total number of lines, words, and characters of multiple text files. In the completion section, we displayed the total count which is the total of the counts for each file.

Share Button

Source: linuxhint.com

Leave a Reply