首页 > 系统服务 > 详细

linux基本命令之grep

时间:2020-06-18 14:10:49      阅读:73      评论:0      收藏:0      [点我收藏+]

概念

其功能是从文本文件或管道数据流中筛选匹配的行及数据,如果再配和正则表达式的技术一起使用,则功能更加强大

语法格式:

技术分享图片

 选项说明:

技术分享图片

正则匹配的主要参数:

\: 忽略正则表达式中特殊字符的原有含义。
^:匹配正则表达式的开始行。
$: 匹配正则表达式的结束行。
\<:从匹配正则表达 式的行开始。
\>:到匹配正则表达式的行结束。
[ ]:单个字符,如[A]即A符合要求 。
[ - ]:范围,如[A-Z],即A、B、C一直到Z都符合要求 。
.:所有的单个字符。
*:所有字符,长度可以为0。

 案例1:使用grep过滤不包含hello的行

/home/user # cat hello.txt                                                                                 
hello world                                                                     
i am student                                                                    
welcome come to china        
                                                   
/home/user # grep -v "hello" hello.txt                                                                                                                                                                                               
i am student                                                                    
welcome come to china                                                                                                                    

案例2:grep命令显示过滤后的内容的行号

/home/user # grep -n "hello" hello.txt                                          
3:hello world  

案例3:利用grep搜索符合要求的用户(查到有r的用户名)

/home/user # grep r ../../etc/passwd                                            
root::0:0::/root:/bin/sh                                                        
sshd:*:27:27:sshd privsep:/var/empty:/sbin/nologin                              
user:CJDfUrYuMxw9.:1000:1000:Linux User,,,:/home/user:/bin/sh 

案例4:配合正则使用,显示所有包含搜索字符串满足i到s的行。

~ $ cat hello.txt                                                               
i am student                                                                    
i am china                                                                      
hello                                                                           
is                                                                              
~ $ grep [i-s] hello.txt                                                      
i am student                                                                    
i am china                                                                      
hello                                                                           
is    

 

linux基本命令之grep

原文:https://www.cnblogs.com/burufeihua/p/13156990.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!