linux最快的文本搜索神器ripgrep(grep的最好代替者)
前言
说到文本搜索工具,大家一定会知道 grep, 它是 linux 最有用并最常用的工具之一。
但如果要再一个大的工程项目中搜索某个关键词,大家也一定知道它比较耗时。
所以就有了很多替代工具,之前最出名的是 Ack,Ag
而最近又有了新的替代者 Ripgrep, 这个工具和 Ack/Ag 一样都使用了多线程的方法,但 rg 比它们更快
简介
ripgrep 是一个以行为单位的搜索工具, 它根据提供的 pattern 递归地在指定的目录里搜索。它是由 Rust 语言写成,相较与同类工具,它的特点就是无与伦比地快。
几个特点如下:
安装 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那样的库的话,一定会极大影响速度。
本篇文章到此结束,如果您有相关技术方面疑问可以联系我们技术人员远程解决,感谢大家支持本站!
相关文章
- Linux下文本复制粘贴技巧实用指南(linux文本复制粘贴)
- 编辑掌握Linux命令行文本编辑的必要之道(linux命令行文本)
- 快速删除Linux中的链接(linux如何删除链接)
- 掌握Linux网络管理技能:成为一名专业的Linux网络管理员(linux网络管理员)
- 利用Linux egrep实现快速文本搜索(linuxegrep)
- Linux下模糊搜索文件的有效利用(linux模糊搜索文件)
- Linux入门:掌握最基础的知识(linux入门知识)
- 达内Linux培训:从入门到精通(达内linux培训视频)
- Linux实现简单文本处理的技巧(linux处理文本)
- 使用Linux Find命令快速搜索文件(linux中find命令)
- Linux如何快速清空文件的命令(linux清空文件命令)
- 【Linux 快速全局搜索文件】(linux全局查找文件)
- Linux分支:开源的创新路径(linux的分支)
- Linux系统关闭防火墙步骤(关闭linux的防火墙)
- Linux的分支:探索开放源代码无穷可能(linux的分支)
- Linux控制蜂鸣器:从无到有的过程(linux控制蜂鸣器)
- 如何快速计算Linux文本文件中的行数?(linux文本行数)
- Linux内存条型号搜索指南(linux内存条型号)
- 优化中文字体在Linux上的显示(中文字体linux)
- Linux 下简单步骤安装MySQL(linux 装mysql)
- 在Linux系统中查看MySQL是否正在运行(linux查看mysql是否运行)