Count number of characters: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 9: Line 9:
# print the newline counts of txt files
# print the newline counts of txt files
wc -l *.txt
wc -l *.txt
# print the whitespaces counts of txt files
grep -c ' ' *.txt
</pre>
</pre>



Revision as of 10:39, 3 October 2019

Count number of characters in different approaches

BASH

Step1: Using Linux wc command

# print the character counts of txt files (contains the count of return symbol)
wc -m *.txt

# print the newline counts of txt files
wc -l *.txt

# print the whitespaces counts of txt files
grep -c ' ' *.txt

Step2: Check the Return symbol

  • e.g. \r\n costs 2 characters

Step3: final formula

Number of characters (not contains the return symbol) = result of wc -m *.txt - result of wc -m *.txt * 2 - 1 (the last blank line costs 1 character)