Sed基本入门[3] Regular Expressions

1、正则表达式基础


 

Begining of line (^)

$ sed -n '/^103/ p' employee.txt 
103,Raj Reddy,Sysadmin 

 

End of line ($)

$ sed -n '/r$/ p' employee.txt 
102,Jason Smith,IT Manager 
104,Anand Ram,Developer 
105,Jane Miller,Sales Manager

 

single Character (.)

$ sed -n 's/J... /Jason /p' employee.txt 
101,Jason Doe,CEO 
105,Jason Miller,Sales Manager

 

Zero or more Occurences (*)

$ vi log.txt 
log: Input Validated 
log:
log:  testing resumed 
log:
log:output created

显示所有包含字符串"log:"并且其后跟有0个或多个空格,然后跟有一个字符的文本行

$ sed -n '/log: *./ p' log.txt
log: Input Validated 
log:  testing resumed
log:output created 

 

One or more Occurences (\+)

显示所有包含字符串"log:"并且其后跟有1个或多个空格的文本行

$ sed -n '/log: \+/ p' log.txt
log: Input Validated 
log:  testing resumed

 

Zero or one Occurence (\?)
显示所有包含字符串"log:"并且其后跟有0个或1个空格的文本行

$ sed -n '/log: \?/ p' log.txt
log: Input Validated 
log: 
log:  testing resumed 
log: 
log:output created

 

Escaping the Special Character (\)

$ sed -n '/127\.0\.0\.1/ p' /etc/hosts 
127.0.0.1        localhost.localdomain localhost

 

Character Class ([0-9])

打印包含数字2、3或4的文本行

$ sed -n '/[234]/ p' employee.txt 
102,Jason Smith,IT Manager 
103,Raj Reddy,Sysadmin 
104,Anand Ram,Developer

或者可以使用如下方法

$ sed -n '/[2-4]/ p' employee.txt 
102,Jason Smith,IT Manager 
103,Raj Reddy,Sysadmin 
104,Anand Ram,Developer 

 

2、其余的正则元字符


 Or Operation (|)

$ sed -n '/101\|102/ p' employee.txt 
101,John Doe,CEO 
102,Jason Smith,IT Manager 

打印包 含数字2到3中的一个数字 或者 包含105的 文本行

$ sed -n '/[2-3]\|105/ p' employee.txt 
102,Jason Smith,IT Manager 
103,Raj Reddy,Sysadmin 
105,Jane Miller,Sales Manager

 

Exactly M Occurrences ({m})

$ vi numbers.txt 
1 
12 
123 
1234 
12345 
123456

打印只包含5个数字的文本行

$ sed -n '/^[0-9]\{5\}$/ p' numbers.txt 
12345 

 

M to N Occurrences ({m,n})

打印只包含3到5个数字的文本行

$ sed -n '/^[0-9]\{3,5\}$/ p' numbers.txt 
123 
1234 
12345 

 

Word Boundary (\b)
代表一个单词的边界

$ cat words.txt 
word matching using: the 
word matching using: thethe 
word matching using: they

打印包含单词the的文本行

$ sed -n '/\bthe\b/ p' words.txt 
word matching using: the 

打印包含以the开头的单词的文本行

$ sed -n '/\bthe/ p' words.txt 
word matching using: the 
word matching using: thethe 
word matching using: they 

 

Back References (\n)

向后引用可以让你在后面使用前面的分组

打印连续包含两次重复the的文本行

sed -n '/\(the\)\1/ p' words.txt 
word matching using: thethe

 

 

转载于:https://www.cnblogs.com/yangfengtao/archive/2013/03/28/2986823.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值