linux系统中grep命令

1、测试数据

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t

2、匹配特定字符串

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep 5 a.txt
5 q k
[root@PC3 test]# grep "w" a.txt
4 w p
6 w f

3、显示特定字符串的前后几行

[root@PC3 test]# grep "5" a.txt
5 q k
[root@PC3 test]# grep -A 2 "5" a.txt  ## 显示匹配5的行及其后两行
5 q k
6 w f
7 x F
[root@PC3 test]# grep -B 3 "5" a.txt  ## 显示匹配5的行及其前3行
2 f a
3 x g
4 w p
5 q k
[root@PC3 test]# grep -C 2 "5" a.txt  ## 显示匹配5的行及其前后各2行
3 x g
4 w p
5 q k
6 w f
7 x F
[root@PC3 test]# grep -A 2 -B 3 "5" a.txt
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F

4、grep -w 精确匹配

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep "a" a.txt  ## 提取匹配a的行
1 e abc
2 f a
[root@PC3 test]# grep -w "a" a.txt  ## 精确匹配包含a的行
2 f a

5、grep -i 忽略大小写

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep "f" a.txt
2 f a
6 w f
[root@PC3 test]# grep -i "f" a.txt  ## 忽略大小写
2 f a
6 w f
7 x F

6、grep同时匹配多个字符

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep "a" a.txt
1 e abc
2 f a
[root@PC3 test]# grep "w" a.txt
4 w p
6 w f
[root@PC3 test]# grep "a\|w" a.txt  ## 同时提取包含a和w的行
1 e abc
2 f a
4 w p
6 w f
[root@PC3 test]# grep -E "a|w" a.txt ## 同时提取包含a和w的行
1 e abc
2 f a
4 w p
6 w f

7、 grep反向选择

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep -v "a" a.txt ## 提取不包含a的行
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep -v "a\|w" a.txt  ## 提取不包含a和w的行
3 x g
5 q k
7 x F
8 m y
9 c t

8、grep提取以特定字符开头和特定字符结尾的行

[root@PC3 test]# cat a.txt 调整测试数据
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
y t f
[root@PC3 test]# grep "^3" a.txt  ## 提取以3开头的行
3 x g
[root@PC3 test]# grep "^r" a.txt  ## 提取以r开头的行
r 7 3
[root@PC3 test]# grep "^3\|^r" a.txt  ## 同时提取以3和以r开头的行
3 x g
r 7 3
[root@PC3 test]# grep -E "^3|^r" a.txt ## 同时提取以3和以r开头的行
3 x g
r 7 3
[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
y t f
[root@PC3 test]# grep "a$" a.txt  ## 提取以a结尾的行
2 f a
[root@PC3 test]# grep "3$" a.txt  ## 提取以3结尾的行
r 7 3
[root@PC3 test]# grep "a$\|3$" a.txt  ## 同时提取以a和以3结尾的行
2 f a
r 7 3
[root@PC3 test]# grep -E "a$|3$" a.txt  ## 同时提取以a和以3结尾的行
2 f a
r 7 3

9、grep命令统计字符出现的次数

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
y t f
[root@PC3 test]# grep -o "f" a.txt
f
f
f
[root@PC3 test]# grep -o "f" a.txt | wc -l  ## 统计f出现的次数
3
[root@PC3 test]# grep -o "w" a.txt
w
w
[root@PC3 test]# grep -o "w" a.txt | wc -l  ## 统计w出现的次数
2
[root@PC3 test]# grep -o "f\|w" a.txt | wc -l   ## 同时统计f和w出现的次数
5
[root@PC3 test]# grep -E -o "f|w" a.txt | wc -l  ## 同时统计f和w出现的次数
5

10、显示字符出现的行数

[root@PC3 test]# cat a.txt
1 e abc
2 f a
3 x g
4 w p
5 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
y t f
[root@PC3 test]# grep -n "a" a.txt ## 显示a出现的行数
1:1 e abc
2:2 f a
[root@PC3 test]# grep -n "f" a.txt ## 显示f出现的行数
2:2 f a
6:6 w f
11:y t f

11、检索包含特定字符串的文件

[root@PC3 test]# ls
a.log  b.log  c.log  d.log
[root@PC3 test]# cat a.log
2 3 f
error!
[root@PC3 test]# cat b.log
dsf d
4 5 s
[root@PC3 test]# cat c.log
f error
d f 8
[root@PC3 test]# cat d.log
e f
ERROR
[root@PC3 test]# grep -l "error" *.log  ## 列出包含error字样的文件
a.log
c.log
[root@PC3 test]# grep -i -l "error" *.log ## 忽略大小写,列出包含error字样的文件
a.log
c.log
d.log
[root@PC3 test]# grep -i -L "error" *.log  ## 忽略大小写,列出不包含error字样的文件
b.log

12、同时提取以特定字符开头和特定字符结尾的行

[root@PC3 test]# cat a.txt  ## 调整测试数据
1 e abc
3 f a
3 x g
4 w k
4 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
r t f
[root@PC3 test]# grep "^3\|g$" a.txt ## 提取以3开头或者以g结尾的行
3 f a
3 x g
[root@PC3 test]# grep -E "^3|g$" a.txt ## 提取以3开头或者以g结尾的行
3 f a
3 x g
[root@PC3 test]# grep "^3.*g$" a.txt  ## 提取以3开头同时以g结尾的行
3 x g
[root@PC3 test]# grep "^4.*k$" a.txt  ## 提取以3开头同时以k结尾的行
4 w k
4 q k

13、grep命令提取以数字或者以字母开头的行

[root@PC3 test]# cat a.txt
1 e abc
3 f a
3 x g
4 w k
4 q k
6 w f
7 x F
8 m y
9 c t
r 7 3
r t f
[root@PC3 test]# grep "^[0-9]" a.txt  ## 提取以数字开头的行
1 e abc
3 f a
3 x g
4 w k
4 q k
6 w f
7 x F
8 m y
9 c t
[root@PC3 test]# grep "^[a-z]" a.txt  ## 提取以字母开头的行,大写字母的话为[A-Z]
r 7 3
r t f
[root@PC3 test]# grep "[0-9]$" a.txt  ## 提取以数字结尾的行
r 7 3
[root@PC3 test]# grep "[a-z]$" a.txt  ## 提取以字母结尾的行
1 e abc
3 f a
3 x g
4 w k
4 q k
6 w f
8 m y
9 c t
r t f

14、grep命令指定匹配字符的开头以及结尾

[root@PC3 test]# cat a.txt
1 abc 45
w xabc 53
8 xabcd 54
[root@PC3 test]# grep "abc" a.txt
1 abc 45
w xabc 53
8 xabcd 54
[root@PC3 test]# grep "\<abc" a.txt  ## 匹配以abc开头的字符
1 abc 45
[root@PC3 test]# grep "abc\>" a.txt  ## 匹配以abc结尾的字符
1 abc 45
w xabc 53
[root@PC3 test]# grep "\<abc\>" a.txt  ## 匹配同时以abc开头和以abc结尾的字符
1 abc 45

15、grep命令剔除空行

[root@PC3 test]# cat a.txt 测试数据
1 abc 45
w xabc 53


8 xabcd 54
[root@PC3 test]# cat -A a.txt
1 abc 45$
w xabc 53$
$
  $
8 xabcd 54$
[root@PC3 test]# sed -n l a.txt
1 abc 45$
w xabc 53$
$
  $
8 xabcd 54$
[root@PC3 test]# cat a.txt
1 abc 45
w xabc 53


8 xabcd 54
[root@PC3 test]# cat -A a.txt  ## a.txt中包含绝对的空行和以空格形式存在的空行
1 abc 45$
w xabc 53$
$
  $
8 xabcd 54$
[root@PC3 test]# nl -b a a.txt  ## 查看行号
     1  1 abc 45
     2  w xabc 53
     3
     4
     5  8 xabcd 54
[root@PC3 test]# grep -v "^$" a.txt  ## 剔除空行
1 abc 45
w xabc 53

8 xabcd 54
[root@PC3 test]# grep -n -v "^$" a.txt  ## 仅剔除了第三行,说明grep可以剔除绝对的空行,不能剔除以空格存在的空行
1:1 abc 45
2:w xabc 53
4:
5:8 xabcd 54
[root@PC3 test]# nl -b a a.txt
     1  1 abc 45
     2  w xabc 53
     3
     4
     5  8 xabcd 54
[root@PC3 test]# awk NF a.txt ## 剔除所有空行
1 abc 45
w xabc 53
8 xabcd 54
[root@PC3 test]# sed '/^[\t ]*$/d' a.txt  ## 剔除所有空行
1 abc 45
w xabc 53
8 xabcd 54

16、匹配整行为特定字符的行

[root@PC3 test]# cat a.txt
1 abc 45
w xabc 53
a
8 xabcd 54
[root@PC3 test]# grep "a" a.txt
1 abc 45
w xabc 53
a
8 xabcd 54
[root@PC3 test]# grep -x "a" a.txt  ## 只有整行为a的行才会显示
a

17、匹配包含连续若干数字或者字母的行

[root@PC3 test]# cat a.txt
1 abc 44325565
w xabc 5344
a
8 xabcd 54
[root@PC3 test]# grep "[0-9]\{2\}" a.txt  ## 提取包含2个连续数字的行
1 abc 44325565
w xabc 5344
8 xabcd 54
[root@PC3 test]# grep "[0-9]\{3\}" a.txt  ## 提取至少包含3个连续数字的行
1 abc 44325565
w xabc 5344
[root@PC3 test]# grep "[0-9]\{4\}" a.txt  ## 4个
1 abc 44325565
w xabc 5344
[root@PC3 test]# grep "[0-9]\{5\}" a.txt  ## 5个
1 abc 44325565
[root@PC3 test]# cat a.txt
1 abc 44325565
w xabc 5344
a
8 xabcd 54
[root@PC3 test]# grep "[a-z]\{2\}" a.txt  ## 至少包含2个连续字母的行
1 abc 44325565
w xabc 5344
8 xabcd 54
[root@PC3 test]# grep "[a-z]\{3\}" a.txt  ## 3个
1 abc 44325565
w xabc 5344
8 xabcd 54
[root@PC3 test]# grep "[a-z]\{4\}" a.txt ## 4个
w xabc 5344
8 xabcd 54
[root@PC3 test]# grep "[a-z]\{5\}" a.txt  ## 5个
8 xabcd 54

18、grep命令同时提取包含多个字符串的行

[root@PC3 test]# cat a.txt
e 8 f
s e g
s g 3
d g w
a g e
3 t r
[root@PC3 test]# grep "g" a.txt
s e g
s g 3
d g w
a g e
[root@PC3 test]# grep "g" a.txt | grep "e"
s e g
a g e
[root@PC3 test]# grep "g" a.txt | grep "e" | grep "a"   ## 提取同时包含a、g和e的行
a g e
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14346968.html