grep is pre-installed on *nix systems and is a tool that programmers often use in their daily work. My recent work involves a lot of large text processing, and grep doesn’t work very fast. So I used ripgrep, a tool that runs much faster. This article is mainly excerpts from the use of their usual may be used.

Install

Just find your corresponding system command in the GitHub Repo guide to install it, one line command.

After installation, the executable command for ripgrep is rg.

Common parameters

1
rg str sample.txt

By default, the standard output will highlight your search term and each line will be preceded by a line number. Rest assured, however, that redirecting to another text does not include line numbers.

If you do not want the line numbers to be output, use `-N.

1
rg str sample.txt -N

Count the number of rows

1
rg str sample.txt -c  # 相当于 grep str sample.txt | wc -l

And conditions

A ha, not yet supported, many users have been mentioning this requirement for a long time. Let’s see when the stubborn author will allow it to be added. You can follow this Issue.

I can only filter again after the pipeline, --color always is so that the first search term can be highlighted too.

1
rg str sample.txt --color always | rg str2

Or conditionals

Use multiple -e arguments as conditions. As long as any search term is matched, it will be matched.

1
rg -e 'str1' -e 'str2' -e 'str3' sample.txt

Stop when you reach N results

In many cases, we know that there are only N matches, and to save time, we want to match enough items to stop searching further.

1
rg str sample.txt -m 数量

Regularity

The following -e can be left out, because the default treats the search term as a regular.

If you wish to treat the search term as ordinary text, use -F, why treat it as a regular by default, magical design, maybe file search uses regulars universally?

1
rg -e '^UE.+?NL' sample.txt

Replace text

Replaces the searched text with the one you specify before output. Use -r.

Note that this parameter does not change your original text, it just replaces it when outputting.

1
rg -e '123456' sample.txt -r '******'

If you wish to implement replacing a specific text of the original file, you can do so.

1
rg --passthru 'like' sample.txt -r 'love'

The -passthru argument will standardize the output of unmatched lines as well.