zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux最快的文本搜索神器ripgrep(grep的最好代替者)

Linux搜索 文本 神器 最好 最快 grep 代替
2023-06-13 09:20:05 时间

前言

说到文本搜索工具,大家一定会知道 grep, 它是 linux 最有用并最常用的工具之一。
但如果要再一个大的工程项目中搜索某个关键词,大家也一定知道它比较耗时。
所以就有了很多替代工具,之前最出名的是 Ack,Ag
而最近又有了新的替代者 Ripgrep, 这个工具和 Ack/Ag 一样都使用了多线程的方法,但 rg 比它们更快

简介

ripgrep 是一个以行为单位的搜索工具, 它根据提供的 pattern 递归地在指定的目录里搜索。它是由 Rust 语言写成,相较与同类工具,它的特点就是无与伦比地快。
几个特点如下:

自动递归搜索 (grep 需要-R) 自动忽略.gitignore 中的文件以及 2 进制文件 可以搜索指定文件类型(rg -tpy foo限定 python 文件, rg -Tjs foo排除 js 文件) 支持大部分 grep 的 feature(常用的都有) 支持各种文件编译(UTF-8, UTF-16, latin-1, GBK, EUC-JP, Shift_JIS 等等) 支持搜索常见压缩文件(gzip, xz, lzma, bzip2, lz4) 自动高亮匹配的结果 更少的命令名称 rg (grep 是四个字符) 不支持多行搜索和花哨的正则

安装 ripgrep

先安装 RUST


curl https://sh.rustup.rs -sSf | sh

然后一路 enter 就好了

用 RUST 安装 rigpre


git clone https://github.com/BurntSushi/ripgrep

cd ripgrep

cargo build --release

cp ./target/release/rg /usr/local/bin/

最后一步根据你的情况把它放到某个在 PATH 里的路径里

使用

搜索结果展示

用法总体格式


USAGE:

 rg [OPTIONS] PATTERN [PATH ...]

 rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...]

 rg [OPTIONS] --files [PATH ...]

 rg [OPTIONS] --type-list

 command | rg [OPTIONS] PATTERN

输入参数

ARGS:
PATTERN
A regular expression used for searching. To match a pattern beginning with a
dash, use the -e/ regexp flag.

For example, to search for the literal -foo , you can use this flag:

rg -e -foo

You can also use the special delimiter to indicate that no more flags
will be provided. Namely, the following is equivalent to the above:

rg -foo

PATH
A file or directory to search. Directories are searched recursively. Paths specified on
the command line override glob and ignore rules.
从文件中读入 pattern, 一行一 pattern 可以被多次使用或和-e 一起组合使用,所以有组合会被匹配

实例二

搜索name为独立单词的内容(-w), 相当于 pattern


$ rg -w "name" ./

实例三

只打印包含匹配内容的文件名(-l)


$ rg -w "name" ./ -l

src/cpp/epoll_server.cpp

src/cpp/uart_xtor.cpp

实例四

只搜索cpp文件(-t), 可以用-T来不搜索某种类型文件


$ rg -w "name" ./ -tcpp

实例五

正则搜索(-e)


$ rg -e "sa.*port" ./ -tcpp

实例六

显示匹配内容及上下各两行(-C), -A/-B类似


$ rg -e "sa.*port" ./ -tcpp -C2

实例七

显示不含 debug 的行(-v)


$ rg -v "debug" -tcpp ./

实例八

只显示匹配部分(-o)


$ rg -e "if.*debug" ./ -tcpp -o

实例九

忽略大小写(-i)


$ rg -ie "if.*debug" ./ -tcpp -o

实例十

把pattern当成常量字符(-F), 像.(){}*+不需要escape,如果要搜索的字符是以-开头,那么要用 来作为分隔符,或者用rg -e "-foo"


rg -F "i++)" ./ -tcpp

实例十一

打印所有将被搜索的文件 files


rg --files

实例十二

输出内置识别文件类型


$ rg --type-list

agda: *.agda, *.lagda

aidl: *.aidl

amake: *.bp, *.mk

asciidoc: *.adoc, *.asc, *.asciidoc

asm: *.S, *.asm, *.s

ats: *.ats, *.dats, *.hats, *.sats

avro: *.avdl, *.avpr, *.avsc

awk: *.awk

bazel: *.bzl, BUILD, WORKSPACE

bitbake: *.bb, *.bbappend, *.bbclass, *.conf, *.inc

bzip2: *.bz2

c: *.H, *.c, *.cats, *.h

cabal: *.cabal

cbor: *.cbor

ceylon: *.ceylon

clojure: *.clj, *.cljc, *.cljs, *.cljx

cmake: *.cmake, CMakeLists.txt

coffeescript: *.coffee

config: *.cfg, *.conf, *.config, *.ini

cpp: *.C, *.H, *.cc, *.cpp, *.cxx, *.h, *.hh, *.hpp, *.hxx, *.inl

creole: *.creole

crystal: *.cr, Projectfile

cs: *.cs

csharp: *.cs

cshtml: *.cshtml

css: *.css, *.scss

csv: *.csv

cython: *.pyx

d: *.d

dart: *.dart

dhall: *.dhall

docker: *Dockerfile*

elisp: *.el

elixir: *.eex, *.ex, *.exs

elm: *.elm

erlang: *.erl, *.hrl

fidl: *.fidl

fish: *.fish

fortran: *.F, *.F77, *.F90, *.F95, *.f, *.f77, *.f90, *.f95, *.pfo

fsharp: *.fs, *.fsi, *.fsx

gn: *.gn, *.gni

go: *.go

groovy: *.gradle, *.groovy

gzip: *.gz

h: *.h, *.hpp

haskell: *.c2hs, *.cpphs, *.hs, *.hsc, *.lhs

hbs: *.hbs

hs: *.hs, *.lhs

html: *.ejs, *.htm, *.html

idris: *.idr, *.lidr

java: *.java, *.jsp

jinja: *.j2, *.jinja, *.jinja2

jl: *.jl

js: *.js, *.jsx, *.vue

json: *.json, composer.lock

jsonl: *.jsonl

julia: *.jl

jupyter: *.ipynb, *.jpynb

kotlin: *.kt, *.kts

less: *.less

license: *[.-]LICEN[CS]E*, AGPL-*[0-9]*, APACHE-*[0-9]*, BSD-*[0-9]*, CC-BY-*, COPYING, COPYING[.-]*, COPYRIGHT, COPYRIGHT[.-]*, EULA, EULA[.-]*, GFDL-*[0-9]*, GNU-*[0-9]*, GPL-*[0-9]*, LGPL-*[0-9]*, LICEN[CS]E, LICEN[CS]E[.-]*, MIT-*[0-9]*, MPL-*[0-9]*, NOTICE, NOTICE[.-]*, OFL-*[0-9]*, PATENTS, PATENTS[.-]*, UNLICEN[CS]E, UNLICEN[CS]E[.-]*, agpl[.-]*, gpl[.-]*, lgpl[.-]*, licen[cs]e, licen[cs]e.*

lisp: *.el, *.jl, *.lisp, *.lsp, *.sc, *.scm

log: *.log

lua: *.lua

lz4: *.lz4

lzma: *.lzma

m4: *.ac, *.m4

make: *.mak, *.mk, GNUmakefile, Gnumakefile, Makefile, gnumakefile, makefile

man: *.[0-9][cEFMmpSx], *.[0-9lnpx]

markdown: *.markdown, *.md, *.mdown, *.mkdn

matlab: *.m

md: *.markdown, *.md, *.mdown, *.mkdn

mk: mkfile

ml: *.ml

msbuild: *.csproj, *.fsproj, *.proj, *.props, *.targets, *.vcxproj

nim: *.nim

nix: *.nix

objc: *.h, *.m

objcpp: *.h, *.mm

ocaml: *.ml, *.mli, *.mll, *.mly

org: *.org

pdf: *.pdf

perl: *.PL, *.perl, *.pl, *.plh, *.plx, *.pm, *.t

php: *.php, *.php3, *.php4, *.php5, *.phtml

pod: *.pod

protobuf: *.proto

ps: *.cdxml, *.ps1, *.ps1xml, *.psd1, *.psm1

puppet: *.erb, *.pp, *.rb

purs: *.purs

py: *.py

qmake: *.prf, *.pri, *.pro

r: *.R, *.Rmd, *.Rnw, *.r

rdoc: *.rdoc

readme: *README, README*

rst: *.rst

ruby: *.gemspec, *.rb, .irbrc, Gemfile, Rakefile

rust: *.rs

sass: *.sass, *.scss

scala: *.sbt, *.scala

sh: *.bash, *.bashrc, *.csh, *.cshrc, *.ksh, *.kshrc, *.sh, *.tcsh, *.zsh, .bash_login, .bash_logout, .bash_profile, .bashrc, .cshrc, .kshrc, .login, .logout, .profile, .tcshrc, .zlogin, .zlogout, .zprofile, .zshenv, .zshrc, bash_login, bash_logout, bash_profile, bashrc, profile, zlogin, zlogout, zprofile, zshenv, zshrc

smarty: *.tpl

sml: *.sig, *.sml

soy: *.soy

spark: *.spark

sql: *.psql, *.sql

stylus: *.styl

sv: *.h, *.sv, *.svh, *.v, *.vg

svg: *.svg

swift: *.swift

swig: *.def, *.i

systemd: *.automount, *.conf, *.device, *.link, *.mount, *.path, *.scope, *.service, *.slice, *.socket, *.swap, *.target, *.timer

taskpaper: *.taskpaper

tcl: *.tcl

tex: *.bib, *.cls, *.ltx, *.sty, *.tex

textile: *.textile

tf: *.tf

toml: *.toml, Cargo.lock

ts: *.ts, *.tsx

twig: *.twig

txt: *.txt

vala: *.vala

vb: *.vb

verilog: *.sv, *.svh, *.v, *.vh

vhdl: *.vhd, *.vhdl

vim: *.vim

vimscript: *.vim

webidl: *.idl, *.webidl, *.widl

wiki: *.mediawiki, *.wiki

xml: *.xml, *.xml.dist

xz: *.xz

yacc: *.y

yaml: *.yaml, *.yml

zsh: *.zsh, .zlogin, .zlogout, .zprofile, .zshenv, .zshrc, zlogin, zlogout, zprofile, zshenv, zshrc

ripgrep的搜索速度真是快的飞起来,在浏览代码时对我的帮助非常大。我相信它对于每一个码农的价值都是无限大的,特别是结合FZF之后。
唯一的弱点是对正则的支持,但这是一个取舍,如果采用如PCRE那样的库的话,一定会极大影响速度。

本篇文章到此结束,如果您有相关技术方面疑问可以联系我们技术人员远程解决,感谢大家支持本站!