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/p/2986823.html