Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of a given character in a file

Tags:

bash

unix

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.

like image 804
mauro Avatar asked Dec 05 '25 00:12

mauro


1 Answers

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
like image 82
anubhava Avatar answered Dec 07 '25 14:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!