How to count the number of specific characters in a file in Linux

How to count the number of specific characters in a file in Linux

Counting the number of a string in a file is actually like looking for stones in a piece of sand. Some people make a mark on the stone after seeing it (grep), and then remember how many marks they made; some people dig it up after seeing the stone (tr), and finally count how many stones they dug; some people jump over the stone after seeing it (awk), and then count how many times they jumped.

This is the file I used

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

I want to match the string 'haha'

1. Grep's -o option

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

At the beginning, the -c option was used, but -c can only count one line. If there are multiple matching strings in a line, -c will be powerless.

This is correct

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

2. awk

This thing is thanks to blackold on CU.

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

-v is used to set the value of a variable. RS is the record separator. The default is new line (\n), which means that awk reads data line by line. But now when RS is 'haha', it reads data as 'haha'. NR is the number of records read. n records are separated by n-1 separators, so it is --NR.

3.tr

Strictly speaking, tr cannot match strings, it can only match single characters. Here we match 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

-d can delete a certain character. If only -d is used, the string after deleting the specific character will be output. However, -c can reverse the display, which means that the deleted characters are displayed. Then you can use wc -c to count the number of characters.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • How to use for loop combined with if to find files in Linux Shell directory
  • How to get the real path of the current script in Linux
  • Detailed explanation of using grep command in Linux
  • Detailed usage of Linux text search command find
  • Detailed explanation of Linux text processing command sort
  • One question to understand multiple parameters of sort command in Linux
  • Linux uses join -a1 to merge two files
  • How to deal with the prompt "Operation not permitted" when deleting files in Linux
  • Several ways to run Python programs in the Linux background
  • Detailed explanation of upgrading Python and installing pip under Linux

<<:  How to query and update the same table in MySQL database at the same time

>>:  Summary of commonly used operators and functions in MySQL

Recommend

How to display div on object without being blocked by object animation

Today I made a menu button. When you move the mous...

Packetdrill's concise user guide

1. Packetdrill compilation and installation Sourc...

Example code for converting http to https using nginx

I am writing a small program recently. Because th...

DOCTYPE Document Type Declaration (Must-Read for Web Page Lovers)

DOCTYPE DECLARATION At the top of every page you w...

Linux remote control windows system program (three methods)

Sometimes we need to remotely run programs on the...

jQuery implements navigation bar effect with expansion animation

I designed and customized a navigation bar with a...

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...

How to reference external CSS files and iconfont in WeChat applet wxss

cause The way to import external files into a min...

vite2.x implements on-demand loading of ant-design-vue@next components

1. Use version vite:2.0 ant-design-vue: 2.0.0-rc....

HTML table tag tutorial (11): horizontal alignment attribute ALIGN

In the horizontal direction, you can set the alig...

Use CSS to draw a file upload pattern

As shown below, if it were you, how would you ach...

How to optimize the slow Like fuzzy query in MySQL

Table of contents 1. Introduction: 2. The first i...