首页 > 其他 > 详细

十八、文本操作——正则表达式

时间:2020-04-06 23:33:59      阅读:138      评论:0      收藏:0      [点我收藏+]
元字符

技术分享图片
(1)查询文件中包含hello的行

user1@SC02ZRC4KMD6N ~ % cat test.txt 
hello testera
hello testerb
Hello testerc
user1@SC02ZRC4KMD6N ~ % grep hello test.txt
hello testera
hello testerb

(2).匹配除换行符外的任意单个字符

user1@SC02ZRC4KMD6N ~ % grep he... test.txt
hello testera
hello testerb

(3)*匹配任意一个跟在它前面的字符

user1@SC02ZRC4KMD6N ~ % grep he.* test.txt
hello testera
hello testerb

注意:前面介绍过通配符*,是可以单独使用的。这里的元字符*是不能单独使用的。
(4)$匹配结尾

user1@SC02ZRC4KMD6N ~ % grep he.*$ test.txt
hello testera
hello testerb

(5)^匹配开头

user1@SC02ZRC4KMD6N ~ % grep ^H test.txt
Hello testerc

(6)[]匹配方括号中的任意一个字符

user1@SC02ZRC4KMD6N ~ % grep ^[Hh] test.txt
hello testera
hello testerb
Hello testerc

(7)\转义后面的特殊字符
(8)正则匹配之后,操作匹配内容:以空格进行分割后取第一个字段

user1@SC02ZRC4KMD6N ~ % grep hello test.txt | cut -d " " -f 1
hello
hello

扩展元字符

技术分享图片

十八、文本操作——正则表达式

原文:https://blog.51cto.com/12936780/2485137

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