[root@linux ~] grep ‘root’ /var/log/secure
将这个文件中有root的那一行显示出来
[root@linux ~] grep –v ‘root’ /var/log/secure
将这个文件中没有root的那一行显示出来
[root@linux ~] grep –v ‘the ‘ test.txt
带有the的行,及其行号一起显示出来
[root@linux ~] grep –i ‘the ‘ test.txt
忽略大小写
[root@linux ~] grep –n‘[g^]oo’ test.txt
将文件中不是goo开头的那一行连同行号显示出来,但必须有oo,可以是foo,too但不是goo
` [root@linux ~] grep –n‘[a-z^]oo’ test.txt
不是小写英文字母+00字和的那一行
[root@linux ~] grep –n‘[0-9]’ test.txt
有数字的那一行
[root@linux ~] grep –n‘^the’ test.txt
单词the只出现在行首
[root@linux ~] grep –n‘^[a-z]’ test.txt
字母开头的行列出来
[root@linux ~] grep –n‘^[^a-Za-Z]’ test.txt
不是英文字母开头的
^[]外表示行首,[^]内表示反向选择
[root@linux ~] grep –n ‘\.$’ test.txt
表示以”.”结尾,由于”.”有特殊含义,所以用”\.”转义
[root@linux ~] grep –n ‘^$’ test.txt
将文本中的空行显示出来
[root@linux ~] grep –v ‘^$’ test.txt | grep –v ‘^#’
=[root@linux ~] egrep –v ‘^$|^#’ test.txt
不是空行和不是以#开头的行,即去注释和空行
0*表示拥有空字符或1个0以上的0
00*第一个0肯定是要存在的第二个0是可有可无的
[root@linux ~] grep –n ‘ooo*’ test.txt
文本中至少有2个连续的o列出来
[root@linux ~] grep –n ‘goo*g’ test.txt
文本中有至少一个o的行列出来
.*代表具有0个或至少一个字符
[root@linux ~] grep –n ‘g.*g’ test.txt
以g开头和结束的字符
[root@linux ~] grep –n ‘[0-9] [0-9]*’ test.txt
具有数字的行
限定字符数
[root@linux ~] grep –n ‘0\{2\}’ test.txt
\为转义符号,对{}转义,表示文本中有2个0的行
[root@linux ~] grep –n ‘go\{2\5}g’ test.txt
G后面接2-5个o,然后再接一个g的行
比较2个文件的差别
[root@linux ~] diff first.txt second.txt 用于文本文件
[root@linux ~] cmp first.txt second.txt 用于二进制文件