In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
When it comes to text search tools, you must know grep, which is one of the most useful and commonly used tools for linux.
But if you want to search for a keyword in a large project, you must know that it is time-consuming.
So there are a lot of alternative tools, the most famous before is Ack,Ag
Recently, there is a new replacement, Ripgrep, which uses the same multithreaded approach as Ack/Ag, but rg is faster than them.
Brief introduction
Ripgrep is a search tool in behavior units that recursively searches the specified directory based on the pattern provided. It is written in the Rust language and is characterized by unparalleled speed compared to similar tools.
Several features are as follows:
Automatic recursive search (grep requires-R) automatically ignores files in gitignore and binary files can search for specified file types (rg-tpy foo qualifies python files, rg-Tjs foo excludes js files) supports most grep feature (commonly used) supports various file compilations (UTF-8, UTF-16) Latin-1, GBK, EUC-JP, Shift_JIS, etc.) support searching for common compressed files (gzip, xz, lzma, bzip2, lz4) automatic highlight matching results less command name rg (grep is four characters) does not support multi-line search and fancy regular
Install ripgrep
Install RUST first
Curl https://sh.rustup.rs-sSf | sh
And then enter all the way.
Install rigpre with RUST
Git clone https://github.com/BurntSushi/ripgrepcd ripgrepcargo build-- releasecp. / target/release/rg / usr/local/bin/
The last step is to put it in a path in PATH depending on your situation.
Use
Display of search results
Overall format of usage
USAGE: rg [OPTIONS] PATTERN [PATH...] Rg [OPTIONS] [- e PATTERN...] [- f PATTERNFILE...] [PATH...] Rg [OPTIONS]-- files [PATH...] Rg [OPTIONS]-- type-list command | rg [OPTIONS] PATTERN
Input parameters
ARGS: 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. A file or directory to search. Directories are searched recursively. Paths specified on the command line override glob and ignore rules.optionsDescriptionother-A,-- after-context shows the line after matching content overwrites-- context-B,-- before-context shows line before matching content overwrites-- context-b,-- byte-offset shows the byte offset of matching content in the file and-o is used together Only print offset-s,-- case-sensitive case sensitivity overrides-I (ignore case),-S (smart case)-- when color uses color, default auto if-vimgre is used, then the default value is never. Optional: never, auto, always, ansi--colors. Set output color: color: red, blue, green, cyan {type}: {attribute}: {value} magenta, yellow, white, black {type}: path, line, column, matchstyle: nobold, bold, nointense {attribute}: fg, bg, styleintense, nounderline, underline {value}: a color or a text styleExample: {type}: none clears the color setting rg-colors' match:fg:magenta'-colors' line:bg:yellow' foo extended color set can be used by {line:bg:yellow' foo} If the terminal supports that the ANSI color description method is'x'(256-color) or'x journal x journal x'(24-bit true color) x is a numeric value between 0 and 255, the default is decimal. 0x prefix is hexadecimal such as: rg-- colors' match:bg:0128255' or equivalent: rg-- colors' match:bg:0x0,0x80,0xFF' intense and nointense are invalid when using extended color code-- the number of columns in which the column first matches (starting from 1) can be canceled by-- no-column-C -- context displays the lines before and after the match, it overrides the-B and-An options-- context-separator is used in the output to separate non-contiguous lines x7F or t can be used The default is-c,-- count shows only the number of matching lines if there is only one file for ripgrep, the number of matching lines can be forced to print the file name with-- with-filename, it overrides the-- count-matches option-- count-matches shows only the number of matches can be used-- with-file to force the file name to be output when there is only one file-- debug displays debugging information-- the upper limit of the dfa-size-limit regex DFA. Default 10M-E,-- encoding description text encoding, default is auto https://encoding.spec.whatwg.org/#concept-encoding-get-f,-- file... To read pattern from a file, a line-by-pattern can be used multiple times or combined with-e, so a combination will be matched-files prints all files to be searched as rg-- files [PATH...] Method can not be added pattern-l,-- files-with-matches only prints with matching file name overwrite-- files-without-match--files-without-match only prints no matching file name overwrite-- file-with-matches-F,-- fixed-strings treats pattern as regular text rather than regex, you can use-- no-fixed-strings to disable this option-L,-- follow will recursively search for links. Off by default, you can use-- no-follow to turn off-g,-- glob. Wildcard files or folders can be used! The reverse can be used multiple times, matching .gitignore 's wildcard rule-h,-- help print help-- heading prints the file name to the top of the match instead of the same line, which is the default behavior, can be turned off with-- no-heading-- hidden search for hidden files and folders is ignored by default, available-- no-hidden off-- iglob... Same as-- glob, but this case-insensitive-I,-- ignore-casepattern case-insensitive can be overridden by-s/--case-sensitive or-S/--smart-case this option-- ignore-file. Ignore the path, the format is the same as .gitignore, but there can be multiple-ignore-file tags, and when the later priority is high on the command, use-g to achieve the same effect-v,-- invert-match reverse matching-n,-- line-number displays the number of file lines Default turns on-x,-- line-regexp only shows lines that match pattern-- word-regexp-M,-- max-columns does not print matching rows longer than columns-- m,-- max-count limits maximum line matching of a file-- max-depth limits folder recursive search depth rg-- max-depth 0 dir/ does not perform any search-- files with max-filesize slightly larger than byte suffix can be K, M G, default is byte--mmap try to use memory maps, default behavior currently it does not support all options, use-- no-mmap to close-- no-config does not read conf file, ignore RIPGREP_CONFIG_PATH--no-filename do not print matching file name-no-heading prints file name before each matching line-no-ignore cancels ignore file For example, .gitignore, .ignore-messages can be closed with-- ignore-- no-ignore-global cancels reading of global ignore files, such as $HOME/.config/git/ignore--no-ignore-messages cancels parsing .ignroe, .gitignore file related errors can be closed by-- ignore-messages-- no-ignore-parent does not read .gitignore in the parent folder. The NUL file can be closed by-- ignore-parent-- no-ignore-vcs can only be omnipotent. Only-matching files can be closed by-- ignore-vcs,-- no-line-number does not print matching lines-- no-messages does not print open and read file-related errors-0,-- null adds a NUL character after the printed file path is very useful for xargs-- o,-- only-matching only prints matching content. Instead of the whole line-- passthru prints matching and mismatched lines-- the path-separator path delimiter, default on linux is /-- pre to process files and give the result to rg there may be a huge performance penalty such as case "$1" in*.pdf) exec pdftotext "$1"- ; *) case $(file "$1") in_Zstandard_) exec pzstd-cdq;;*) exec cat;;esac;;esac-p,-- pretty--color always-- heading-- line-number-q,-- quiet does not print to stdout, if a match is found, stopping rg is very useful when rg is used for exit code-- regex-size-limit compiles the upper limit of regex-e,-- regexp. Use regular matching to match can use this option multiple times, print lines that match any pattern can be used to search for-beginning pattern, such as rg-e-foo-r,-- replace prints out the group serial number with the corresponding file instead of matching content ($5) can be used-z,-- search-zip search in the gz,bz2,xz,lzma,lz4 file type can be turned off by-- no-search-zip-S,-- smart-case if all lowercase Is case-insensitive, otherwise sensitive can be turned off through-s/--case-sensitive and-i/--ignore-case-- sort-files sorts output results by file path will close parallel search thread-- stats prints out statistical results-text search binaries can be turned off-- j via-- no-text,-- approximate number of threads used by threads-- t,-- type... Search only for a certain file type. You can use-- type-lsit to list the supported file types-- type-add... Adding file types such as rg-type-add 'foo:*.foo'-tfoo PATTERN can also be used to create a rule that contains multiple file types-type-add' src:include:cpp,py,md'--type-clear. Clear the default file type-type-list lists all built-in file types-T,-- type-not... Do not search for a certain file type-u,-unrestricted-u search for files in .gitignore,-uu search for hidden files-uuu search for binaries-V,-- version print version information-- vimgrep prints multiple lines per line per match-H,-- with-filename prints the matching file path By default, you can use-- no-filename to turn off-w,-- word-regexp to match pattern as a separate word, and
< >Equivalent
Example display
Example one
$rg 'name'. /
Example two
Search for content in which name is an independent word (- w), which is equivalent to
$rg-w 'name'. /
Example three
Print only the file name containing the matching content (- l)
$rg-w 'name'. /-lsrc/cpp/epoll_server.cppsrc/cpp/uart_xtor.cpp
Example 4
Search only cpp files (- t), you can use-T not to search for certain types of files
$rg-w 'name'. /-tcpp
Example five
Regular search (- e)
$rg-e "sa.*port". /-tcpp
Example 6
Display matching content and two lines above and below (- C), similar to-A _ color _ B
$rg-e "sa.*port". /-tcpp-C2
Example 7
Show lines without "debug" (- v)
$rg-v "debug"-tcpp.
Example 8
Show only the matching part (- o)
$rg-e "if.*debug". /-tcpp-o
Example 9
Ignore case (- I)
$rg-ie "if.*debug". /-tcpp-o
Example 10
Think of pattern as a constant character (- F), such as. () {} * + does not need escape. If the character to be searched starts with -, use-- as the delimiter, or use rg-e "- foo".
Rg-F "iTunes +)". /-tcpp
Example 11
Print all the files to be searched-- files
Rg-files
Example 12
Output built-in identification file type
$rg-- type-listagda: * .agda, * .lagdaaidl: * .aidlamake: * .bp, * .mkasciidoc: * .adoc, * .asc, * .asciidocasm: * .S, * .asm, * .sats: * .ats, * .dats, * .hats, * .satsavro: * .avdl, * .avpr, * .avscawk: * .awkbazel: * .bzl, BUILD, WORKSPACEbitbake: * .bb, * .bbappend, * .bbclass, * conf. * .incbzip2: * .bz2c: * .H, * .c, * .cats, * .hcabal: * .cabalcbor: * .cborceylon: * .ceylonclojure: * .clj, * .cljc, * .cljs, * .cljxcmake: * .cmake, CMakeLists.txtcoffeescript: * .coffeeconfig: * .cfg, * .conf, * .config, * .inicpp: * .C, * .H, * .cc, * .cpp, * .cxx, * h * .hh, * .hpp, * .hxx, * .inlcreole: * .creolecrystal: * .cr, Projectfilecs: * .cscsharp: * .cscshtml: * .cshtmlcss: * .css, * .scsscsv: * .csvcython: * .pyxd: * .ddart: * .dartdhall: * .dhalldocker: * Dockerfile*elisp: * .elelixir: * .eex, * .ex, * .exselm: * .elmerlang: * .erl * .hrlfidl: * .fidlfish: * .fishfortran: * .F, * .F77, * .F90, * .F95, * .f90, * .f77, * .f90, * .f95, * .pfofsharp: * .fs, * .fsi, * .fsxgn: * .gn, * .gnigo: * .gogroovy: * .gradle, * .groovygzip: * .gzh: * .h, * .hpphaskell: * .c2hs, * .cpphs, * .hs * .hsc, * .lhshbs: * .hbshs: * .hs, * .lhshtml: * .ejs, * .htm, * .htmlidris: * .idr, * .lidrjava: * .java, * .jspjinja: * .j2, * .jinja, * .jinja2jl: * .jljs: * .js, * .jsx, * .vuejson: * .json, composer.lockjsonl: * .jsonljulia: * .jljupyter: * .ipynb, * .jpynbkotlin: * .kt * .ktsless: * .lesslicense: * [. -] LICEN [CS] ejaculation, 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] *, Linc [CS] E Licen [CS] E [. -] *, MIT-* [0-9] *, MPL-* [0-9] *, NOTICE, NOTICE [. -] *, OFL-* [0-9] *, PATENTS, PATENTS [. -] *, UNLICEN [CS] E, UNLICENN [CS] E [. -] *, agpl [. -] *, gpl [. -] *, lgpl [. -] *, licen [cs] e, licen [cs] e.*lisp: *. El, *. Jl, *. Lisp, *. Lsp * .sc, * .scmlog: * .loglua: * .lualz4: * .lz4lzma: * .lzmam4: * .ac, * .m4make: * .mak, * .mk, GNUmakefile, Gnumakefile, Makefile, gnumakefile, makefileman: *. [0-9] [cEFMmpSx], *. [0-9lnpx] markdown: * .markdown, * .md, * .mdown, * .mkdnmatlab: * .mmd: * .markdown, * .md, * mdown. * .mkdnmk: mkfileml: * .mlmsbuild: * .csproj, * .fsproj, * .proj, * .props, * .targets, * .vcxprojnim: * .nimnix: * .nixobjc: * .h, * .mobjcpp: * .h, * .mmocaml: * .ml, * .mli, * .mll, * .mlyorg: * .orgpdf: * .pdfperl: * .PL, * .perl, * .pl, * .plh, * .plx, * .pm * .tphp: * .php, * .php3, * .php4, * .php5, * .phtmlpod: * .podprotobuf: * .protops: * .cdxml, * .ps1, * .ps1xml, * .psd1, * .psm1puppet: * .erb, * .pp, * .rbpurs: * .purspy: * .pyqmake: * .prf, * .pri, * .pror: * .R, * .Rmd, * Rnw. * .rrdoc: * .rdocreadme: * README, README*rst: * .rstruby: * .gemspec, * .rb, .irbrc, Gemfile, Rakefilerust: * .rssass: * .sass, * .scssscala: * .sbt, * .scalash: * .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, zshrcsmarty: * .tplsml: * .sig, * .smlsoy: * .soyspark: * .sparksql: * psql, * .sqlstylus: * .stylsv: * .h, * .sv, * .svh * .v, * .vgsvg: * .svgswift: * .def, * .isystemd: * .automount, * .conf, * .device, * .link, * .mount, * .path, * .scope, * .service, * .slice, * .socket, * .swap, * .target, * .timertaskpaper: * .taskpapertcl: * .tcltex: * .bib, * .cls, * .ltx, * .sty * .textextile: * .textiletf: * .tftoml: * .toml, Cargo.lockts: * .ts, * .tsxtwig: * .twigtxt: * .txtvala: * .valavb: * .vbverilog: * .sv, * .svh, * .v, * .vhvhdl: * .vhd, * .vhdlvim: * .vimvimscript: * .vimwebidl: * .idl, * .webidl, * .widlwiki: * .mediawiki, * .wikixml: * .xml * .xml.distxz: * .xzyacc: * .yyaml: * .yaml, * .ymlzsh: * .zsh, .zlogin, .zlogout, .zprofile, .zshenv, .zshrc, zlogin, zlogout, zprofile, zshenv, zshrc
Summary
The search speed of ripgrep is really fast, and it helps me a lot when browsing the code. I believe its value to every programmer is infinite, especially when combined with FZF.
The only weakness is support for regularities, but this is a trade-off, and if you use a library like PCRE, it will greatly affect speed.
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.