更新时间:

#linux grep命令

linux grep 命令

  • 从文件中读取关键词进行搜索 cat test.txt | grep -nf test2.txt
  • 输出test.txt文件中含有从test2.txt文件中读取出的关键词的内容行,行号是输出的test文件的
  • 从多个文件中查找关键词 grep 'linux' test.txt test2.txt
  • 根据文件内容递归查找目录
    1. grep -r ‘energywise’ * 在当前目录及其子目录下搜索'energywise'行的文件
    2. grep -l -r ‘energywise’ * 在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件
  • 输出非u开头的行内容 cat test.txt |grep ^[^u]

#grep 参数

-n  行号
-m  限制输出行数 (每个文件查找到限制个数)
-l   小写L 只显示匹配文件
-L  显示不匹配文件
-c  计算匹配行数
-f  从-f text2 中查找要匹配的关键词   规则文件
-A  匹配行之后几行
-B  匹配行之前
-C  匹配行前后
-E   egrep  扩展的正则表达式
-i  忽略大小写
-v  反向选择
-r   --recursive   #此参数的效果和指定“-d recurse”参数相同
-w   --word-regexp   #只显示全字符合的列。 <完整的> test.db 也可以只匹配db
-0 小写字母 o 只输出匹配到的内容, 用于检测有没有这个东西
-c  显示一共匹配了多少行,  不需要再 |wc -l ; 但是一般情况下,得测试下输出是否正确,再,拿到正确命令, 再用这个命令
 
--color=auto :可以将找到的关键词部分加上颜色的显示喔   建议alias改为默认

  • cat note | grep ‘a | b’ 或者 cat test.txt |grep -E "ed|at"
  • grep 2种写法 cat ..| grep 或者 grep ‘’ file1 file2...
    1. 建议第二种 直接grep
  • 忽略子目录:grep -d skip
  • fgrep = grep -F 它利用固定的字符串来对文本进行搜索,但不支持正则表达式的引用,所以此命令的执行速度也最快。
  • grep 正则 grep 'd{7,}’ 大括号也需要 '转义'
  • 查找前面是rc中间接任意字符而后跟/rc

grep '.*\(rc\).*\/\1.*' /etc/inittab

  • 显示/etc/inittab 中以#开头,且后面跟一个或者多个空白符,而后又跟了任意非空白符的行

grep '^#[[:space:]]*[^[:space:]]' /etc/inittab 用这种技巧统计一个或者多个

  • 显示/etc/inittab文件中以一个数字开头并以一个与开头数字相同的数字结尾的行

grep '^\([0-9]\).*\1$' /etc/inittab 或者 grep '^\([[:digit:]]\).*\1$' /etc/inittab

  • 将/etc/passwd,将没有出现 root 和nologin的行取出来.2个任何一个都不能有
    grep -v root /etc/passwd | grep -v nologin
  • 输出行首是1或2
    grep -E '^(1|2)' /etc/inittab
  • 搜索多个文件并查找匹配文本在哪些文件中:
    grep -l "text" file1 file2 file3...

#grep与正规表达式

#字符类
  1. 字符类的搜索:如果我想要搜寻 test 或 taste 这两个单字时,可以发现到,其实她们有共通的 't?st' 存在~这个时候,我可以这样来搜寻:
 grep -n 't[ae]st' regular_express.txt
    8:I can't finish the test.
    9:Oh! The soup taste good.

其实 [] 里面不论有几个字节,他都谨代表某『一个』字节, 所以,上面的例子说明了,我需要的字串是『tast』或『test』两个字串而已!

  1. 字符类的反向选择 [^] :如果想要搜索到有 oo 的行,但不想要 oo 前面有 g,如下
[root@www ~]# grep -n '[^g]oo' regular_express.txt
2:apple is my favorite food.
3:Football game is not use feet only.
18:google is the best tools for search keyword.
19:goooooogle yes!

第 2,3 行没有疑问,因为 foo 与 Foo 均可被接受!
但是第 18 行明明有 google 的 goo 啊~别忘记了,因为该行后面出现了 tool 的 too 啊!所以该行也被列出来~ 也就是说, 18 行里面虽然出现了我们所不要的项目 (goo) 但是由於有需要的项目 (too) , 因此,是符合字串搜寻的喔!
至於第 19 行,同样的,因为 goooooogle 里面的oo 前面可能是 o,例如: go(ooo)oogle ,所以,这一行也是符合需求的!

  1. 字符类的连续: 假设我 oo 前面不想要有小写字节,所以,我可以这样写 [^abcd....z]oo , 但是这样似乎不怎么方便,由於小写字节的 ASCII 上编码的顺序是连续的, 因此,我们可以将之简化为底下这样:[root@www ~]#grep-n'[^a-z]oo'

也就是说,当我们在一组集合字节中,如果该字节组是连续的,例如大写英文/小写英文/数字等等, 就可以使用[a-z],[A-Z],[0-9]等方式来书写,那么如果我们的要求字串是数字与英文呢? 呵呵!就将他全部写在一起,变成:[a-zA-Z0-9]。
我们要取得有数字的那一行,就这样:

[root@www ~]# grep -n '[0-9]' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.
  1. 行首与行尾字节 ^ $

行首字符:如果我想要让 the 只在行首列出呢? 这个时候就得要使用定位字节了!我们可以这样做:

[root@www ~]# grep -n '^the' regular_express.txt
12:the symbol '*' is represented as start.

此时,就只剩下第 12 行,因为只有第 12 行的行首是 the 开头啊~此外, 如果我想要开头是小写字节的那一行就列出呢?可以这样:

[root@www ~]# `grep -n '^[a-z]' regular_express.txt`
2:apple is my favorite food.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
12:the symbol '*' is represented as start.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

如果我不想要开头是英文字母,则可以是这样:

[root@www ~]# grep -n '^[^a-zA-Z]' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird
^ 符号,在字符类符号(括号[])之内与之外是不同的! 在 [] 内代表『反向选择』,在 [] 之外则代表定位在行首的意义!

那如果我想要找出来,行尾结束为小数点 (.) 的那一行:

[root@www ~]# grep -n '\.$' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
10:motorcycle is cheap than car.
11:This window is clear.
12:the symbol '*' is represented as start.
15:You are the best is mean you are the no. 1.
16:The world <Happy> is the same with "glad".
17:I like dog.
18:google is the best tools for search keyword.
20:go! go! Let's go.

特别注意到,因为小数点具有其他意义(底下会介绍),所以必须要使用转义字符()来加以解除其特殊意义!

#找出空白行:
[root@www ~]# grep -n '^$' regular_express.txt
22:
因为只有行首跟行尾 (^$),所以,这样就可以找出空白行啦!

任意一个字节 . 与重复字节 *
这两个符号在正则表达式的意义如下:

. (小数点):代表『一定有一个任意字节』的意思;
* (星号):代表『重复前一个字符, 0 到无穷多次』的意思,为组合形态

假设我需要找出 g??d 的字串,亦即共有四个字节, 起头是 g 而结束是 d ,我可以这样做:

[root@www ~]# grep -n 'g..d' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".
因为强调 g 与 d 之间一定要存在两个字节,因此,第 13 行的 god 与第 14 行的 gd 就不会被列出来啦!

如果我想要列出有 oo, ooo, oooo 等等的数据, 也就是说,至少要有两个(含) o 以上,该如何是好?
因为 代表的是『重复 0 个或多个前面的 RE 字符』的意义, 因此,『o』代表的是:『拥有空字节或一个 o 以上的字节』,因此,『 grep -n 'o*' regular_express.txt 』将会把所有的数据都列印出来终端上!
当我们需要『至少两个 o 以上的字串』时,就需要 ooo* ,亦即是:

[root@www ~]# grep -n 'ooo*’ regular_express.txt //最后一个o有没有都可以
1:"Open Source" is a good mechanism to develop programs.
2:apple is my favorite food.
3:Football game is not use feet only.
9:Oh! The soup taste good.
18:google is the best tools for search keyword.
19:goooooogle yes!

如果我想要字串开头与结尾都是 g,但是两个 g 之间仅能存在至少一个 o ,亦即是 gog, goog, gooog.... 等等,那该如何?

[root@www ~]# grep -n 'goo*g' regular_express.txt
18:google is the best tools for search keyword.
19:goooooogle yes!

如果我想要找出 g 开头与 g 结尾的行,当中的字符可有可无

[root@www ~]# grep -n 'g.*g' regular_express.txt
1:"Open Source" is a good mechanism to develop programs.
14:The gd software is a library for drafting programs.
18:google is the best tools for search keyword.
19:goooooogle yes!
20:go! go! Let's go.

因为是代表 g 开头与 g 结尾,中间任意字节均可接受,所以,第 1, 14, 20 行是可接受的喔! 这个 .* 的 RE 表示任意字符是很常见的.

如果我想要找出『任意数字』的行?因为仅有数字,所以就成为:

[root@www ~]# grep -n '[0-9][0-9]*' regular_express.txt
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

上边例子 来自博客园,具体出处不确定了..