Linux正则表达式

1. 什么是正则表达式?

简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法

通过定义的这些特殊符号的辅助,系统管理员就可以快速过滤,替换或输出需要的字符串。Linux正则表达式一般以行为单位处理。

 

linux的正则表达式,最常应用正则表达式的命令就是grep、egrpe、sed、awk,换句话说linux三剑客要想能工作的更高效,那一定离不开正则表达式的配合。

正则表达式和我常用的通配符特殊字符是有本质区别的。

 

注意事项:

1. linux正则表达式一般以行为单位处理

2.  alias grep='grep --color=auto'

3. 注意字符集export LC_ALL=C    查看字符集:echo $LC_ALL

 

正则表达式字符说明:

1. ^word 匹配以word开头的内容。 vi/vim编辑器里^代表一行的开头

2. word$ 匹配以word结尾的内容。 vi/vim编辑器里$代表一行的结尾

3. ^$    表示空行

4. .     代表且只能代表任意一个字符

5. \     转义字符

6. *     重复0个或者多个前面的一个字符

7. .*    匹配所有字符。^.*以任意多个字符开头 .*$以任意多个字符结尾

   点(.)的特殊含义:

       1.当前目录  2.使得文件生效相当于source 3.隐藏文件 4.任意一个字符(grep正则表达式)

8. [abc] 匹配字符集合内的任意一个字符[a-z,A-Z],[0-9]

9. [^abc] 匹配不包括^后的任意一个字符的内容

10. a\{n,m} 重复n到m次,前一个重复的字符。如果用egrep/sed -r可以去掉斜线

  a\{n,\} 重复至少n次,前一个重复的字符

  a\{n\}  重复n次,前一个重复的字符

  a\{,m\}

egrep(grep -E)或sed -r过滤一般特殊字符可以不转义\

例:

1.过滤出来以T开头的行

[root@centos ~]# grep "^T" ett.txt      

There is a very classic example about the importance of credit.

2. 过滤出来以e结尾的行

[root@centos ~]# grep "e$" ett.txt   

 He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

3.过滤空行

[root@centos ~]# grep -n "^$" ett.txt

1:

3:

4:

7:

4.除空行以外的行

[root@centos ~]# grep -vn "^$" ett.txt  

2:There is a very classic example about the importance of credit.

5: A Chinese man took the train in Europe.

6: He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

8: money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

5.匹配字符.

[root@centos ~]# grep -n "." ett.txt   

2:There is a very classic example about the importance of credit.

5: A Chinese man took the train in Europe.

6: He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

8: money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

6.匹配所有字符.*

[root@centos ~]# grep -n ".*" ett.txt

1:

2:There is a very classic example about the importance of credit.

3:

4:

5: A Chinese man took the train in Europe.

6: He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

7:

8: money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

7.只匹配点.使用\转义符

[root@centos ~]# grep -n "\.$" ett.txt         

2:There is a very classic example about the importance of credit.

5: A Chinese man took the train in Europe.

8.匹配一个或者多个e

[root@centos ~]# grep -n "e*" ett.txt

1:

2:There is a very classic example about the importance of credit.

3:

4:

5: A Chinese man took the train in Europe.

6: He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

7:

8: money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

9.grep使用颜色选项,会匹配abc

[root@centos ~]# grep "[abc]" ett.txt          

There is a very classic example about the importance of credit.

 A Chinese man took the train in Europe.

 He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

 money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

10.grep使用颜色选项,会匹配不包括abc以外的字符

[root@centos ~]# grep "[^abc]" ett.txt         

There is a very classic example about the importance of credit.

 A Chinese man took the train in Europe.

 He found no one checked the ticket, so he just skipped the ticket. He thought he had saved the little

 money, but when he came to the bank and wanted to get the loan, he got rejected, because his record of credit showed he had skipped the ticket, which made him the unreliable man.

11.

[root@centos ~]# grep "0\{3\}" ett.txt

0000,4444

my qq num is 450000485

my is 450000485

[root@centos ~]# grep "0\{3,\}" ett.txt

0000,4444

my qq num is 450000485

my is 450000485

[root@centos ~]# grep "0\{3,4\}" ett.txt

0000,4444

my qq num is 450000485

my is 450000485

 

扩展正则表达式使用命令为 grep -E或egrep

1. +重复一个或一个以上前面的字符

2. ?重复0个或一个0前面的字符

3. |用或的方式查找多个符合的字符串

4. ()分组过滤,后向引用

 

元字符:

\b   单词边界 例\bcool\b匹配cool,不匹配lcools

\n   换行符   例\n 匹配新行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值