Linux之ack命令

举报
入门小站 发表于 2022/03/17 21:55:52 2022/03/17
【摘要】 回复【1001】获取 linux常用命令速查手册ack是比grep好用的文本搜索工具 ack命令安装> yum install -y ack 命令特点默认搜索当前工作目录默认递归搜索子目录忽略元数据目录,比如.svn,.git,CSV等目录忽略二进制文件(比如pdf,image,coredumps)和备份文件(比如foo~,*.swp)在搜索结果中打印行号,有助于找到目标代码能搜索特定文件类...

回复【1001】获取 linux常用命令速查手册

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

搜索有特殊字符的字符串’ p a t h = . , 所有的元字符(比如’ path=.’,所有的元字符(比如’ ’,’.’)需要在字面上被匹配:

> ack -Q '$path=.' /etc

除了temp目录,在所有目录搜索use单词

> ack use --ignore-dir=temp

只搜索包含’main’单词的Python文件,然后通过文件名把搜索结果整合在一起,打印每个文件对应的搜索结果

> ack  --python  --group -w main

image-20210327222106648

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/

原文链接:https://rumenz.com/rumenbiji/linux-ack.html
微信公众号:入门小站

  • 回复【1001】获取 linux常用命令速查手册
  • 回复【10010】获取 阿里云ECS运维Linux系统诊断
  • 回复【10012】获取 Linux学习笔记【强悍总结值得一看】
  • 回复【10013】获取 shell简明教程

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200