美文网首页
三剑客_grep

三剑客_grep

作者: 慕知 | 来源:发表于2021-01-05 15:32 被阅读0次

grep

  • grep 相关参数
1,过滤包含root
[root@m01~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin




2,-i 不区分大小写过滤
[root@m01~]# grep -i "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin




3,-n 显示行数
[root@m01~]# grep -n "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin




4,-w 过滤单词root
[root@m01~]# grep -w "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

例:
[root@m01~]# cat xx.txt
abc123
abc\123
abc 123
ab*c
#  注意 此处abc123是个整体,当作一个单词

[root@m01~]# grep -w 'abc' xx.txt
abc\123
abc 123
# 把abc当作一个整体词汇来过滤



补充
# 过滤出 22端口
[root@m01~]# netstat -an | grep "22" # 会包含22端口都会过滤出

[root@m01~]# netstat -an | grep -w "22"
[root@m01~]# netstat -an | grep "\<22\>"

注意:-w 单词是指连续的一个值,比如root_user





5,-o 只过滤出搜索的关键词内容
[root@m01~]# grep -o "root" /etc/passwd
root
root
root
root



6,-A 过滤出关键词及下面2行内容
[root@m01~]# grep -A 2 "root" /etc/passwd
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin






7,-B 过滤出关键词及上面2行内容
[root@m01~]# grep -B 2 "root" /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin






8,-C 过滤出关键词上下各两行内容
[root@m01~]# grep -C 2 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin







9,-c 过滤出包含关键词的总行数
[root@m01~]# grep -c 'root' /etc/passwd
2





10,-v 取反
[root@m01~]# grep -v 'root' /etc/passwd

[root@m01~]# ps aux | grep [s]sh
等同于
[root@m01~]# ps aux | grep ssh | grep -v grep






11,递归过滤出包含root的文件(重要且实用)
[root@m01~]# grep -rl 'root' /etc/







12,-E 支持扩展正则
grep -E 等同于 egrep
  • grep + 正则表达式(扩展正则)
[root@m01~]# cat cc.txt
abbbb
ababaa
bba
bbbbbc
bcaaab



1,^以什么开头
[root@m01~]# grep '^a' cc.txt
abbbb
ababaa




2,$以什么结尾
[root@m01~]# grep 'c$' cc.txt
bbbbbc





3,. 任意一个字符
[root@m01~]# grep 'a.b' cc.txt
abbbb
bcaaab





4,*  左边的字符出现0次或者无穷次;等同于 {0,}
[root@m01~]# grep 'ab*' cc.txt
abbbb
ababaa
bba
bcaaab

[root@m01~]# egrep 'ab{0,}' cc.txt
abbbb
ababaa
bba
bcaaab





5 , + 左边的字符出现1次或者无数次;等同于 {1,}
[root@m01~]# egrep "ab+" cc.txt
abbbb
ababaa
bcaaab


[root@m01~]# egrep "ab{1,}" cc.txt
abbbb
ababaa
bcaaab


#中括号也可以自定义筛选出的内容次数
[root@m01~]# egrep "ab{1,3}" cc.txt
abbbb
ababaa
bcaaab






6, .* 任意字符(0或无穷次) -- 贪婪匹配
[root@m01~]# grep 'a.*b' cc.txt
abbbb
ababaa
bcaaab
# 注意 ,正则是贪婪的,a.b 会匹配到最后一个b






7,.* 任意字符(0或无穷次) -- 非贪婪匹配
    grep -P,  .*?代表非贪婪 

[root@m01~]# cat ee.txt
url="www.baidu.com" >> "百度"
url="www.sina.com" >> "新浪"

[root@m01~]# grep -P 'url=".*?"' ee.txt
url="www.baidu.com" >> "百度"
url="www.sina.com" >> "新浪"

#  url="www.sina.com" 会有红色标记







8, 匹配[ ]里的内容    或
[root@m01~]# cat ww.txt
12ww3
as---jh3
hads233331

[root@m01~]# grep '[123]' ww.txt
12ww3
as---jh3
hads233331
# 以上含有1或者2或者3的都会红色标记
# 搜索的内容切记加上单引号或双引号;搜索多个不需要加空格或者分号

[root@m01~]# grep [a-zA-Z] ww.txt
等同于
[root@m01~]# grep '[a-Z]' ww.txt

也可以如下使用
[root@m01~]# grep '[1-9][1-9]' ww.txt







9, ( )及 | 的用法;筛选出company和companies
[root@m01~]# cat qq.txt
123company
234companies
dhfhk


[root@m01~]# egrep 'compan(y|ies)' qq.txt
123company
234companies
# 以上company和companies 会高亮提示



补充用法:
[root@m01~]# cat vv.txt
singablesing
hhhhppppppking

[root@m01~]# egrep "(sing)able\1" vv.txt
singablesing
#  分组,  \1代表引用括号里的值








10,过滤出 含空格及转行的内容
[root@m01~]# cat vv.txt
ab
le
sing    ggggg
dhjsfjhk


[root@m01~]# grep -Pzo 'ab\nle' vv.txt
ab
le

[root@m01~]# grep -Pzo 'sing\tggggg' vv.txt
sing    ggggg



==============================================




补充注意:
[root@m01~]# cat ww.txt
a+b
a-b
a*b
a/b
aaab

[root@m01~]# grep a[-+/*]b ww.txt
a+b
a-b
a*b
a/b
# 以上取出加减乘除的值,注意-号不能放在中间,会代表范围(类似1-9)


1),   取反: [^  ]             ;                
[root@m01~]# grep a[^-+/*]b ww.txt
aaab


2),   ^[ ]  以什么开头
[root@m01~]# grep ^a[-+/*]b ww.txt
a+b
a-b
a*b
a/b

相关文章

网友评论

      本文标题:三剑客_grep

      本文链接:https://www.haomeiwen.com/subject/ajeioktx.html