I need to count - in bash - the number of a given (single byte) character in a file. For example: count the number of commas, or dots or uppercase 'C' or... any other character.
Basically I need a generalized version of wc -l to count any single byte character (not just new lines) contained in a certain file.
I have to use it with very large files (several GB) so it has to be fast and resource efficient. Ideally the same level of performances you have with wc -l if you had to count new-lines.
You can use grep -o with wc -l. e.g. to count # of letter C in your input file:
grep -Fo 'C' file | wc -l
To get this done in single command you can use gnu awk with custom RS:
awk -v RS='C' 'END{print NR-1}' file
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With