Linux method of counting the number of specific characters in a file

  • 2021-01-03 21:15:35
  • OfStack

To count the number of a string in a file is to look for a stone in a piece of sand. Some people, after seeing the stone, mark it (grep) and remember how many marks they have made. Some people see the stone, dig it (tr), and finally count how many stones they have dug; Some people see a stone, jump it over (awk), and count how many times they have jumped.

This is the file I used


[root@bzhou test]# cat file
hafsdha
hahafsdfsdhaha
haha

I want to match the string 'haha'

1. -ES13en option for grep


[root@bzhou test]# grep -c 'haha' file
2

We started with -c, but -c can only count one row, and if there are multiple matching strings in one row, -c won't be able to do it.

This is correct


[root@bzhou test]# grep -o 'haha' file | wc -l
3

2. awk

Thanks to blackold on CU for this.


[root@bzhou test]# awk -v RS='haha' 'END {print --NR}' file

-ES33en to set the value of 1 variable, RS is the record delimiter, default is new line (\n), that is, awk read data as 1 line 1, but now RS is 'haha', NR is the number of records read, n records are separated by ES42en-1 delimiter, so it is --NR.

3.tr

Strictly speaking, tr does not match strings, only individual characters. This matches the number of 'h' in this file.


[root@bzhou test]# tr -cd 'h' <file | wc -c
8
[root@bzhou test]# grep -o 'h' file | wc -l
8

-ES54en can delete a character. If -ES55en is the only character, it will output the deleted character string, but -ES56en can be inverted, which shows the deleted character. Then you can use ES57en-ES58en to count the number of characters.

conclusion


Related articles: