ack是比grep好用的文本搜索工具
ack命令安装
> yum install -y ack
命令特点
- 默认搜索当前工作目录
- 默认递归搜索子目录
- 忽略元数据目录,比如.svn,.git,CSV等目录
- 忽略二进制文件(比如pdf,image,coredumps)和备份文件(比如foo~,*.swp)
- 在搜索结果中打印行号,有助于找到目标代码
- 能搜索特定文件类型(比如Perl,C++,Makefile),该文件类型可以有多种文件后缀
- 高亮搜索结果
- 支持Perl的高级正则表达式,比grep所使用GNU正则表达式更有表现力。
相比于搜索速度,ack总体上比grep更快。ack的速度只要表现在它的内置的文件类型过滤器。在搜索过程中,ack维持着认可的文件类型的列表,同时跳过未知或不必要的文件类型。它同样避免检查多余的元数据目录。
命令参数
- -n, 显示行号
- -l/L, 显示匹配/不匹配的文件名
- -c, 统计次数
- -v, invert match
- -w, 词匹配
- -i, 忽略大小写
- -f, 只显示文件名,不进行搜索.
- -h, 不显示名称
- -v, 显示不匹配
在当前目录递归搜索单词”eat”,不匹配类似于”feature”或”eating”的字符串:
> ack -w eat
搜索有特殊字符的字符串’path=.’,所有的元字符(比如’’,’.’)需要在字面上被匹配:
> ack -Q \'$path=.\' /etc
除了temp目录,在所有目录搜索use单词
> ack use --ignore-dir=temp
只搜索包含’main’单词的Python文件,然后通过文件名把搜索结果整合在一起,打印每个文件对应的搜索结果
> ack --python --group -w main
ack支持的文件类型
> ack --help-types--[no]actionscript .as .mxml --[no]ada .ada .adb .ads --[no]asm .asm .s --[no]asp .asp --[no]aspx .master .ascx .asmx .aspx .svc --[no]batch .bat .cmd --[no]cc .c .h .xs --[no]cfmx .cfc .cfm .cfml --[no]clojure .clj .cljs .edn .cljc --[no]cmake CMakeLists.txt; .cmake --[no]coffeescript .coffee --[no]cpp .cpp .cc .cxx .m .hpp .hh .h .hxx --[no]csharp .cs --[no]css .css --[no]dart .dart --[no]delphi .pas .int .dfm .nfm .dof .dpk .dproj .groupproj .bdsgroup .bdsproj --[no]elisp .el --[no]elixir .ex .exs --[no]erlang .erl .hrl --[no]fortran .f .f77 .f90 .f95 .f03 .for .ftn .fpp --[no]go .go --[no]groovy .groovy .gtmpl .gpp .grunit .gradle --[no]gsp .gsp --[no]haskell .hs .lhs --[no]hh .h --[no]hpp .hpp .hh .h .hxx --[no]html .htm .html .xhtml --[no]jade .jade --[no]java .java .properties --[no]js .js --[no]json .json --[no]jsp .jsp .jspx .jspf .jhtm .jhtml --[no]kotlin .kt .kts --[no]less .less --[no]lisp .lisp .lsp --[no]lua .lua; first line matches /^#!.*\\blua(jit)?/ --[no]make .mk; .mak; makefile; Makefile; Makefile.Debug; Makefile.Release --[no]matlab .m --[no]objc .m .h --[no]objcpp .mm .h --[no]ocaml .ml .mli .mll .mly --[no]parrot .pir .pasm .pmc .ops .pod .pg .tg --[no]perl .pl .pm .pod .t .psgi; first line matches /^#!.*\\bperl/ --[no]perltest .t --[no]php .php .phpt .php3 .php4 .php5 .phtml; first line matches /^#!.*\\bphp/ --[no]plone .pt .cpt .metadata .cpy .py --[no]python .py; first line matches /^#!.*\\bpython/ --[no]rake Rakefile --[no]rr .R --[no]rst .rst --[no]ruby .rb .rhtml .rjs .rxml .erb .rake .spec; Rakefile; first line matches /^#!.*\\bruby/ --[no]rust .rs --[no]sass .sass .scss --[no]scala .scala --[no]scheme .scm .ss --[no]shell .sh .bash .csh .tcsh .ksh .zsh .fish; first line matches /^#!.*\\b(?:ba|t?c|k|z|fi)?sh\\b/ --[no]smalltalk .st --[no]smarty .tpl --[no]sql .sql .ctl --[no]stylus .styl --[no]swift .swift; first line matches /^#!.*\\bswift/ --[no]tcl .tcl .itcl .itk --[no]tex .tex .cls .sty --[no]ts .ts .tsx --[no]tt .tt .tt2 .ttml --[no]vb .bas .cls .frm .ctl .vb .resx --[no]verilog .v .vh .sv --[no]vhdl .vhd .vhdl --[no]vim .vim --[no]xml .xml .dtd .xsd .xsl .xslt .ent .wsdl; first line matches /<[?]xml/ --[no]yaml .yaml .yml
获取包含CFLAG关键字的Makefile的文件名
> ack --make CFLAG
ack查找my.cnf文件
> ack -f /etc/ | ack my.cnf//或者> ack -g my.cnf /etc/
© 版权声明
本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!
THE END