grep 用法总结

grep: http://www.gnu.org/software/grep/manual/html_node/index.html

rg

rg查找变量定义,使用正则,整词匹配,类型指定

rg -w 'struct net \{' -th

rg 查函数声明

rg -w '[^()]\s*ip_hdr\(.*\)$' -th

grep

总览 SYNOPSIS
       grep [options] PATTERN [FILE...]
       grep [options] [-e PATTERN | -f FILE] [FILE...]

       grep [选项]  要匹配的字符串  [文件或者路径]

OPTIONS

Matching Control 匹配控制

-e PATTERN, --regexp=PATTERN

指定【PATTERN】,为了区分【PATTERN】中含有 “-” 的情况

-f FILE, --file=FILE
从文件中读取【PATTERN】,每行一个。

-i, --ignore-case
忽略大小写

-v, --invert-match
反向匹配, 打印无 【PATTERN】的行。

-w, --word-regexp
选中包含【PATTERN】的行,其中【PATTERN】是整个单词。

-x, --line-regexp
选中包含【PATTERN】的行,其中【PATTERN】是占整个行,无别的内容。

General Output Control 输出控制

-c, --count
只打印文件中匹配行的个数,不打印行内容。

--color[=WHEN], --colour[=WHEN]
WHEN is never, always, or auto.

-L, --files-without-match
打印无【PATTERN】的文件名。

-l, --files-with-matches
只打印含【PATTERN】的文件名。

-q, --quiet, --silent
无打印,正常返回0

-s, --no-messages
不打印,无法读取之类的错误消息。

ipv4

grep -E "^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?)(.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9]?)){3}$"
grep -E "^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$"

Output Line Prefix Control 输出行前缀控制

 -b, --byte-offset
          在输出的每行前面同时打印出当前行在输入文件中的字节偏移量。
   -H, --with-filename
          Print the file name for each match.  This is the default when there is more than one file to search.
        打印每个匹配行的文件名,多个文件时候默认打开

   -h, --no-filename
          Suppress the prefixing of file names on output.  This is the default when there is only one file (or only standard input) to search.
        不打印每个匹配行的文件名,单个文件时候默认打开

    -n, --line-number
          Prefix each line of output with the 1-based line number within its input file.
        打印行号

-T, --initial-tab
用tab 把文件名,行号,内容隔开,并对齐。

Context Line Control,前后行控制

    -A NUM, --after-context=NUM
          Print NUM lines of trailing context after matching lines.  Places a line containing a group separator  (--)  between  contiguous  groups  of
          matches.  With the -o or --only-matching option, this has no effect and a warning is given.
        打印匹配行 后的 NUM。

   -B NUM, --before-context=NUM
          Print  NUM  lines  of  leading  context before matching lines.  Places a line containing a group separator (--) between contiguous groups of
          matches.  With the -o or --only-matching option, this has no effect and a warning is given.
        打印匹配行 前面的 NUM。

   -C NUM, -NUM, --context=NUM
          Print NUM lines of output context.  Places a line containing a group separator (--) between contiguous groups of matches.  With  the  -o  or
          --only-matching option, this has no effect and a warning is given.
        打印匹配行前后的 NUM 行,等价于同时设  -A  -B

File and Directory Selection

-d ACTION, --directories=ACTION

If an input file is a directory, use ACTION to process it.
如果输入文件是一个目录,就用 ACTION 去处理它,默认是read,
If ACTION is skip, silently skip directories.如果是skip,就跳过,
If ACTION is recurse, read all files under each directory,如果是recurse,就递归读入每个文件。This is equivalent to the -r option.

-R, -r, --recursive
          递归地读每一目录下的所有文件。这样做和 -d recurse 选项等价。

    --include=PATTERN
          仅仅在搜索匹配 PATTERN 的文件时在目录中递归搜索。

     --exclude=PATTERN
          在目录中递归搜索,但是跳过匹配 PATTERN 的文件。

-s, --no-messages
禁止输出关于文件不存在或不可读的错误信息。

shell

ref

大括号、花括号 {}

(1) 大括号拓展。(通配(globbing))将对大括号中的文件名做扩展。在大括号中,不允许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展作用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt

# ls {ex1,ex2}.sh    
ex1.sh  ex2.sh    
# ls {ex{1..3},ex4}.sh    
ex1.sh  ex2.sh  ex3.sh  ex4.sh    
# ls {ex[1-3],ex4}.sh    
ex1.sh  ex2.sh  ex3.sh  ex4.sh    

(2) 代码块,又被称为内部组,这个结构事实上创建了一个匿名函数 。与小括号中的命令不同,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必须要有一个空格。

demo

grep

当前目录下的所有文件, *号不能少,匹配所有文件。
grep objStr ./*

当前目录下所有文件,递归搜索,只需要指定目录。
grep objStr -r ./

指定一个匹配类型
grep objStr -r ./ --include=*.h

指定多个匹配类型
grep objStr -rl ./ --include=*.{cpp,c}

grep combine sed

变量替换。

sed -i 's/gBpfShareData/g_bpfShareData/g'   `grep gBpfShareData -rl ./ --include=*.{cpp,c,h}`

posted on 2019-02-23 07:36  ims-  阅读(609)  评论(0编辑  收藏  举报

导航