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:
Word Counter
The wc command by default outputs the number of newlines, words, and bytes of the file:
|
|
Byte Counts
We can add the -c or –bytes parameter to output the file’s byte counts:
|
|
Character Counts
We can add the -m or –chars parameter to output the file’s character counts:
|
|
Word Counts
We can add the -w or –words parameter to output the file’s word counts:
|
|
Newline Counts
We can add the -l or –lines parameter to output the file’s line counts:
|
|
Sorted Result
Sort One File Contents in Ascending Order
We can use the sort command to sort the letters of the file contents:
|
|
Sort Multiple File Contents in Ascending Order
We can sort multiple files at the same time:
|
|
Sort Contents in Descending Order
We can add the -r or –reverse parameter to sort the contents of the file in descending order:
|
|
Sort Numeric Contents in Ascending Order
Before we start, we need to prepare a file with numeric contents:
|
|
We can add the -n, –numeric-sort or –sort=numeric parameter to sort the integer number of the file 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.
|
|
References 6.1 wc: Print newline, word, and byte counts, 7.1 sort: Sort text files
Author Dong Chen
LastMod Mon Feb 25 2019