
The wc command in Linux is an important tool used to count words, lines, and characters in files or standard input (STDIN). It's also known as word count (wc) and enables the processing of various text operations such as analyzing file sizes, counting lines in documents, or calculating average word lengths.
This article explains how to use the wc command in Linux to manipulate and analyze text data.
wc Command SyntaxBelow is the basic wc command syntax:
Within the above command, [file...] specifies one or more files to process. If no files are provided, wc reads from standard input.
wc Command Optionswc CommandCount the number of words in a file.
The above command counts the number of words in file.txt.
Output:
Count the number of lines in a file.
The above command counts the number of lines in the file.txt file.
Output:
Count the number of characters in a file.
The above command counts the number of characters in the file.txt file.
Output:
Count words, lines, and characters together.
The above command displays the count of lines, words, and characters in the file.txt file.
Output:
Count the number words in multiple files.
The above command counts the number of words in the specified files file1.txt and file2.txt.
Output:
Count the number of lines in multiple files.
The above command counts the number of lines in both file1.txt and file2.txt.
Output:
Count the number of characters in multiple files.
The above command counts the number of characters in the specified files file1.txt and file2.txt.
Output:
Count words from standard input.
The above command counts the number of words from standard input provided by the echo command.
Output:
Count the number of lines from standard input.
The above command counts the number of lines from standard input.
Output:
Count characters from standard input.
The above command counts the number of characters from standard input.
Output:
Count words, lines, and characters recursively in directories.
The above command recursively counts the number of words, lines, and characters in all files within the working directory and subdirectories.
Output:
Exclude specific files.
The above command uses the find command to locate all files except .txt files and counts the number of words in each file.
Output:
Count the number of words in a specific column of a CSV file.
The above command extracts the third column of a comma-separated values (CSV) file using the cut utility and counts the number of words in the column.
Output:
Count the length of the longest line in a file.
The above command displays the length of the longest line in the file.txt file.
Output:
Combine wc with grep to count specific patterns.
The above command counts the occurrences of a specific pattern in the file.txt file using grep to match the pattern and wc to count the number of output lines.
Output:
Combine wc with find to count lines in multiple directories.
The above command counts the number of lines in all files within multiple directories specified by the find command.
Output:
Count all non-empty lines in a file.
The above command counts the number of non-empty lines in the file.txt file using grep to exclude empty lines and count them.
Output:
Generate a summary report using wc.
The above command generates a summary report showing the number of lines, words, and characters in the files file1.txt and file2.txt.
Output:
You have used the wc command to perform basic and complex count processing operations. For more options, run the man wc command to view the wc manual page.
0 Comments
Be the first to comment and share your perspective with the community.