Regular Expression

REGULAR EXPRESSION
1. 正则表达式和通配符是不同的两个概念

  通配符中的*代表任意字符,而RE中的*则代表重复数。
   通配符的反向选择为!,而RE中为^
   通配符中?代表单个字符,而RE中.代表单个字符
  
2. grep是最常用的利用RE的工具
   grep -option 'targetString' filename
   -c: 计算找到的次数
   -i: 忽略大小写
   -n:输出行号
   -v: 反向选择,即显示不符合搜索条件的行
  
   fredchen@fredchen:~$ grep -n 't[ae]st' regular_express
   8:I can't finish the test.
   9:Oh! The soup taste good.
   1) [ae]: a或e
  
   2) '[^g]oo' : 00前面不是g
  
   fredchen@fredchen:~$ grep -n '[^g]oo' regular_express
   2:apple is my favorite food.
   3:Football game is not use feet only.
   18:google is the best tools for search keyword.
   19:goooooogle yes!
   注意最后两行,虽然存在oo前面含有g的字符串但还是被筛选,因为有满足条件的字符串存在!
  
   3)行首与行尾^, $
   fredchen@fredchen:~$ grep -n '^the' regular_express
   12:the symbol '^' is represented as start.
    
   ^在[]内外的含义完全不同,要注意 
   行首不是字母的行
   fredchen@fredchen:~$ grep -n '^[^a-zA-Z]' regular_express
   1:"Open source" is a good mechanism to develop programs.
   21:# I am VBird
  
   以.结尾的
   fredchen@fredchen:~$ grep -n '/.$' regular_express     
   1:"Open source" is a good mechanism to develop programs.
   2:apple is my favorite food.                                
   3:Football game is not use feet only.                  
   4:this dress doesn't fit me.                           
   5:However, this dress is about $ 3183 dollars.         
   6:GNU is free air not free beer.    
  
   找出空白行:相当于只有行尾和行首
   fredchen@fredchen:~$ grep -n '^$' regular_express                     
   22:     
  
   去掉代码的空行和注释行:
   fredchen@fredchen:~$ grep -v '^$' /etc/syslog.conf | grep -v '^#'                                                   
   auth,authpriv.*                 /var/log/auth.log                        
   *.*;auth,authpriv.none          -/var/log/syslog                         
   daemon.*                        -/var/log/daemon.log                     
   kern.*                          -/var/log/kern.log                       
   lpr.*                           -/var/log/lpr.log                        
   mail.*                          -/var/log/mail.log                       
   user.*                          -/var/log/user.log                       
   mail.info                       -/var/log/mail.info                      
   mail.warning                    -/var/log/mail.warn                      
   mail.err                        /var/log/mail.err                        
   news.crit                       /var/log/news/news.crit                  
   news.err                        /var/log/news/news.err                   
   news.notice                     -/var/log/news/news.notice               
   *.=debug;/                                                               
           auth,authpriv.none;/                                             
           news.none;mail.none     -/var/log/debug                          
   *.=info;*.=notice;*.=warning;/                                           
           auth,authpriv.none;/                                             
           cron,daemon.none;/                                               
           mail,news.none          -/var/log/messages                       
   *.emerg                         *                                        
   daemon.*;mail.*;/                                                        
           news.err;/                                                       
           *.=debug;*.=info;/                                               
           *.=notice;*.=warning    |/dev/xconsole 
                                    
   4)任意字符.  重复字符*
   注:*可以表示空字符 ,如x*则表示0个或多个x。而xx*才表示一个或多个
   fredchen@fredchen:~$ grep -n 'g..d' regular_express                              
   1:"Open source" is a good mechanism to develop programs.                                                                      
   9:Oh! The soup taste good.                             
   16:The world <Happy> is the same with "glad".          
  
   含有两个或多个o的行                               
   fredchen@fredchen:~$ grep -n 'ooo*' regular_express    
   1:"Open source" is a good mechanism to develop programs.
   2:apple is my favorite food.                           
   3:Football game is not use feet only.                  
   9:Oh! The soup taste good.                             
   18:google is the best tools for search keyword.        
   19:goooooogle yes!                        
  
   含g...g型的字符串的行
   fredchen@fredchen:~$ grep -n 'g.*g' regular_express                 
   1:"Open source" is a good mechanism to develop programs.
   14:The gd software is a library for drafting programs. 
   18:google is the best tools for search keyword.        
   19:goooooogle yes!                                     
   20:go! go! Let's go.
  
   任意数字'[0-9][0-9]*'
  
   5) 限定连续重复字符范围
   2个到5个o的goo..g型字符串
   fredchen@fredchen:~$ grep -n 'go/{2,5/}g' regular_express
   18:google is the best tools for search keyword.    
  
   如果确定重复数,则显然可以用*更方便

3. 文件比较diff
  -i:忽略大小写
  -b:忽略一行中多个空白的差异
  -B:忽略空白行的差异
  
    fredchen@fredchen:~$ diff newfile  new2
    3d2                                   
    < zyx                                 
    fredchen@fredchen:~$ cat newfile      
    I'm fredchen                          
    This is the last line                 
    zyx                                   
    ujl                                   
    rejrl                                 

    fredchen@fredchen:~$ cat new2         
    I'm fredchen                          
    This is the last line                 
    ujl                                   
    rejrl                                 
4. cmp 也可用于文件比较,不过用得不如diff广泛
    diff一行为单位比较,cmp按位比较,可用于比较二进制文件
5. 打印机打印: pr
    pr /etc/man.config

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值