【转载】Grep命令详解-9个经典使用场景

Grep
全称Global Regular Expression Print,表示全局正则表达式
是一个强大的文本搜索工具,采用正则匹配
1、命令格式
grep [options] files


2、主要参数
-c: 只输出匹配行的数目
-i: 不区分大小写
-n:显示匹配航以及行号
-l:查询多文件的时候只输出包含匹配字符的文件名
-v:反向匹配,即显示不匹配的行
-h: 查询的时候不适用文件名
-s:不显示错误信息


3、部分正则表达式
\     反义字符:如"\"\""表示匹配""
^$ 开始和结束
[] 单个字符,[A]
[ - ] 匹配一个范围,[0-9a-zA-Z]匹配所有数字和字母
* 前面的字符出现0次或者多次
+ 前面的字符出现了一次或者多次
. 任意字符

4、经典场景

除非要精确区分大小写,否则请加上-i来忽略大小写

(1)结合find命令和管道
  你的一个音乐文件夹里有多种格式的文件,而你只想找到艺术家jay的mp3文件,并且不含有任何的混合音轨
[root@localhost ~]#find . -name ".mp3" | grep -i jay | grep -vi "remix"
分析: 1)使用find -name 来列出所有mp3文件,重定向给grep
2) 使用grep -i 来查找包含jay的行
3)使用grep -vi 来查找不包含remix的行


(2)-A -B -C
  很多时候,我们并关心匹配行而是关心匹配行的上下文。这时候-A -B -C就有用了
-A n 后n行,A记忆为(After)
-B n 前n行,B记忆为(Before)
-C n 前n行,后n行,C记忆为(Center)
举例
[root@localhost ~]# ifconfig | grep -A 2 "Link encap"
eth0      Link encap:Ethernet  HWaddr 00:0C:29:F3:38:15  
          inet addr:192.168.91.129  Bcast:192.168.91.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fef3:3815/64 Scope:Link
--
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


[root@localhost ~]#  ifconfig | grep -C 2 "lo"
          Interrupt:67 Base address:0x2024 


lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host


(3) 用-c来统计数目
  你手头有一个很大的文件,这个文件包含网址,比如www.baidu.com tieba.baidu.com等等。你想要知道有多少个隶属于百度的网址
[
root@localhost ~]# grep -c "*baidu.com*" filename
例子
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -cn ".*baidu.com.*" file.txt 
3

(4) -r 递归查找子目录
  查找当前目录极其子目录下面包含匹配字符的文件
查找子目录,匹配后输出行号,这里的点表示当前目录
[root@localhost ~]# grep -nr HELLO_HWC_CSND_BLOG* .

例子:

[root@localhost ~]# grep -nr baidu .
./file.txt:8:www.baidu.com
./file.txt:9:tieba.baidu.com
./file.txt:11:www.baidu.com/search/index
./test/test.txt:1:http://www.baidu.com

查找子目录,匹配后只输出文件名
[root@localhost ~]# grep -lr HELLO_HWC_CSND_BLOG* .
例子:

[root@localhost ~]# grep -lr baidu .
./file.txt
./test/test.txt


(5)--line-buffered 打开buffering 模式
  你有一个文件是动态的,它不断地添加信息到文件的尾部,而你想要输出包含某些信息的行。即持续的grep一个动态的流


[root@localhost ~]#tail -f file | grep --line-buffered your_pattern 


(6)结合ps查找进程
[root@localhost ~]# ps aux | grep init
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients
root      4956  0.0  0.1   3920   680 pts/1    R+   23:27   0:00 grep init

这里我们看到了grep init我们执行的命令也被列出来了
如果不想要这一行,我们可以这么改命令
[root@localhost ~]# ps aux | grep [i]nit
root         1  0.0  0.1   2072   632 ?        Ss   22:52   0:01 init [5]                             
root      4210  0.0  0.1   6508   620 ?        Ss   23:01   0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "/usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients"
root      4233  0.0  0.0   2780   504 ?        S    23:01   0:00 /usr/bin/dbus-launch --exit-with-session /etc/X11/xinit/Xclients

(7)查找不包含某一个目录
[root@localhost ~]#grep -R --exclude-dir=node_modules 'some pattern' /path/to/search
例子

[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  file.txt  find.result  install.log  install.log.syslog  test
[root@localhost ~]# grep -r baidu .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
./test/test.txt:http://www.baidu.com
这时候如果我们不想包含test目录
[root@localhost ~]# grep -R --exclude-dir=text "baidu" .
./file.txt:www.baidu.com
./file.txt:tieba.baidu.com
./file.txt:www.baidu.com/search/index
如果报错
grep: unrecognized option `--exclude-dir=test'
说明版本过老,更新下就ok

(8)查找IP地址
这里用到了-o和-P命令
我们通过man grep查看
-o, --only-matching:
              Show only the part of a matching line that matches PATTERN.
-P, --perl-regexp:
              Interpret PATTERN as a Perl regular expression.
也就是说-o,只显示匹配行中匹配正则表达式的那部分
-P,作为Perl正则匹配
[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "([0-9]{1,3}\.){3}[0-9]{1,3}" file.txt
192.168.0.1
162.12.0.123

(9)查找邮箱

[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt

例子

[root@localhost ~]# cat file.txt
wtmp begins Mon Feb 24 14:26:08 2014
192.168.0.1
162.12.0.123
"123"
123""123
njuhwc@163.com
njuhwc@gmil.com 123
www.baidu.com
tieba.baidu.com
www.google.com
www.baidu.com/search/index
[root@localhost ~]# grep -oP "[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+" file.txt
njuhwc@163.com
njuhwc@gmil.com
————————————————
版权声明:本文为CSDN博主「黄文臣」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Hello_Hwc/article/details/40017833

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用\[1\]:ps -aux命令是一种常用的进程查看命令。其中,-a选项表示显示所有用户的进程,-u选项表示显示进程的详细信息,-x选项表示显示没有控制终端的进程。而grep命令是一种文本搜索工具,用于在文件中查找指定的字符串。所以,ps -aux | grep命令的作用是通过ps命令查看所有用户的进程,并通过grep命令筛选出包含指定字符串的进程。\[1\] 引用\[2\]:ps -aux命令是用来查看当前系统中所有进程的命令。其中,-a选项表示显示所有用户的进程,-u选项表示显示进程的详细信息,-x选项表示显示没有控制终端的进程。而|符号是管道符号,用于将前一个命令的输出作为后一个命令的输入。grep命令是一种文本搜索工具,用于在文件中查找指定的字符串。所以,ps -aux | grep命令的作用是通过ps命令查看所有用户的进程,并通过grep命令筛选出包含指定字符串的进程。\[2\] 问题:ps -aux |grep命令详解 回答:ps -aux | grep命令是一种常用的进程查看和筛选命令。ps -aux命令用于查看当前系统中所有进程的详细信息,包括进程的用户、进程ID、CPU占用率等。而grep命令则用于在ps命令的输出结果中筛选出包含指定字符串的进程。通过将ps -aux命令的输出结果通过管道符号|传递给grep命令,可以实现对进程的筛选和搜索。这个命令在系统管理和故障排查中经常被使用,可以帮助用户找到特定的进程或者了解系统中的进程情况。 #### 引用[.reference_title] - *1* [linux命令ps aux|grep xxx详解](https://blog.csdn.net/weixin_41789688/article/details/112845439)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [ps -aux | grep 用法详解](https://blog.csdn.net/weixin_36099503/article/details/114909629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值