Is there any way under linux/terminal to count, how many times the char f occurs in a plain text file?
-
9Technically this could be considered a sh/bash/etc. programming question, so I think it has validity in either place. – Rob Hruska Oct 21 '09 at 21:51
-
@Rob Hruska: yes, I also think is bash programming... @abrashka: the answer for your first and second question is "NO"! – cupakob Oct 22 '09 at 07:33
5 Answers
How about this:
fgrep -o f <file> | wc -l
Note: Besides much easier to remember/duplicate and customize, this is about three times (sorry, edit! botched the first test) faster than Vereb's answer.

- 479,068
- 72
- 370
- 318
-
This one doesn't work if you need to count `\r` or `\n` characters; the `tr -cd f` answer does work for that. – bjnord Oct 05 '13 at 00:08
-
3To count several characters, e.g. `a`, `b` and `c`, use `egrep` : `egrep -o 'a|b|c'
| wc -l`. – Skippy le Grand Gourou Apr 03 '17 at 13:29 -
Also, beware to NOT use `wc -c` as in the `tr` answer : since `grep` outputs line by line, `wc` would count end-of-lines as characters (hence doubling the number of characters). – Skippy le Grand Gourou Apr 03 '17 at 13:34
-
@bjnord Ok for `\r`, but to count `\n` why not just use `wc -l` ? – Skippy le Grand Gourou Apr 03 '17 at 13:35
-
Warning: `fgrep` is obsolescent; use `grep -F`. e.g. `grep -oF f
| wc -l` – Qumber Nov 19 '22 at 09:49
even faster:
tr -cd f < file | wc -c
Time for this command with a file with 4.9 MB and 1100000 occurences of the searched character:
real 0m0.089s
user 0m0.057s
sys 0m0.027s
Time for Vereb answer with echo
, cat
, tr
and bc
for the same file:
real 0m0.168s
user 0m0.059s
sys 0m0.115s
Time for Rob Hruska answer with tr
, sed
and wc
for the same file:
real 0m0.465s
user 0m0.411s
sys 0m0.080s
Time for Jefromi answer with fgrep
and wc
for the same file:
real 0m0.522s
user 0m0.477s
sys 0m0.023s

- 2,278
- 1
- 23
- 30

- 1,032
- 8
- 7
-
3To count several characters, e.g. `a`, `b` and `c` : `tr -cd abc < file | wc -l`. – Skippy le Grand Gourou Apr 03 '17 at 13:26
-
1are you sure? wasn't suppose to be `tr -cd abc < file | wc -c` instead – Mithun B May 09 '20 at 18:36
echo $(cat <file> | wc -c) - $(cat <file> | tr -d 'A' | wc -c) | bc
where the A is the character
Time for this command with a file with 4.9 MB and 1100000 occurences of the searched character:
real 0m0.168s
user 0m0.059s
sys 0m0.115s
-
1This gets about a third faster if you take out the unnecessary `cat` s, giving the filename as an argument to `wc` and `tr`. – Cascabel Oct 21 '09 at 21:49
-
1If you realy want to optimize this reads the file just once: echo $(stat -c%s
) - $(cat – Vereb Oct 21 '09 at 22:01| tr -d 'A' | wc -c) | bc -
@Vereb - tr only reads `stdin`, but that can be piped rather than `cat`ed: `tr -d 'A' <
| wc ...` – dsz Nov 16 '15 at 04:28
If all you need to do is count the number of lines containing your character, this will work:
grep -c 'f' myfile
However, it counts multiple occurrences of 'f' on the same line as a single match.

- 2,721
- 2
- 27
- 36

- 97
- 1
- 1
tr -d '\n' < file | sed 's/A/A\n/g' | wc -l
Replacing the two occurrences of "A" with your character, and "file" with your input file.
tr -d '\n' < file
: removes newlinessed 's/A/A\n/g
: adds a newline after every occurrence of "A"wc -l
: counts the number of lines
Example:
$ cat file
abcdefgabcdefgababababbbba
1234gabca
$ tr -d '\n' < file | sed 's/a/a\n/g' | wc -l
9

- 118,520
- 32
- 167
- 192