24. Bash Shell - Text Processing: wc, sort
Contents
We can use the wc, sort command provided by Linux to count the number of words, the number of bytes, and the sort of letters and numbers.
We continue to use the file file.txt test data we used in Entry Level post:

The contents of the file.txt file
Word Counter
The wc command by default outputs the number of newlines, words, and bytes of the file:
|
|

Outputs the length of the newlines, words and bytes of the file.txt file
Byte Counts
We can add the -c or –bytes parameter to output the file’s byte counts:
|
|

Outputs the byte counts of file.txt file
Character Counts
We can add the -m or –chars parameter to output the file’s character counts:
|
|

Outputs the char counts of file.txt file
Word Counts
We can add the -w or –words parameter to output the file’s word counts:
|
|

Outputs the word counts of file.txt file
Newline Counts
We can add the -l or –lines parameter to output the file’s line counts:
|
|

Outputs the line counts of file.txt file
Sorted Result
Sort One File Contents in Ascending Order
We can use the sort command to sort the letters of the file contents:
|
|

Outputs the sorted contents of the file.txt file
Sort Multiple File Contents in Ascending Order
We can sort multiple files at the same time:
|
|

Outputs the sorted contents of the file.txt file
Sort Contents in Descending Order
We can add the -r or –reverse parameter to sort the contents of the file in descending order:
|
|

Outputs the reversed sorted contents of the file.txt file
Sort Numeric Contents in Ascending Order
Before we start, we need to prepare a file with numeric contents:
|
|

Save a few lines of integer numbers to the num_file file
We can add the -n, –numeric-sort or –sort=numeric parameter to sort the integer number of the file contents in ascending order.
|
|

Output numeric contents in ascending order
Sort Numeric Contents in Descending Order
We can add the -n, –numeric-sort or –sort=numeric with a -r or –reverse parameter to sort the integer number of the file contents in ascending order.
|
|

Output numeric contents in descending order
References 6.1 wc: Print newline, word, and byte counts, 7.1 sort: Sort text files
Author Dong Chen
LastMod Mon Feb 25 2019