学习Grep,Sed中的正则 | 火丁笔记

学习Grep,Sed中的正则 | 火丁笔记

 


学习Grep,Sed中的正则

 

正则要是几天不学习不用功,肯定会忘得一干二净,下面聊聊Grep,Sed中的正则。

 

问题:从一个文本文件里筛选出含有电话号码的行,电话号码是由七位或者八位阿拉伯数字组成(开头不是零),并且被单引号或者双引号包裹。

鉴于问题的需要,先杜撰一份数据:

shell> cat /path/to/data.txt
'7654321'
'7654321"
"87654321"
"87654321'

失败的尝试

shell> grep "(['\"])[1-9][0-9]{6,7}\1" /path/to/data.txt
grep: Invalid back reference
shell> sed -n "/(['\"])[1-9][0-9]{6,7}\1/p" /path/to/data.txt
sed: -e expression #1, char 25: Invalid back reference

成功的尝试

使用Basic Regular Expressions (BRE)

shell> grep "\(['\"]\)[1-9][0-9]\{6,7\}\1" /path/to/data.txt
'7654321'
"87654321"
shell> sed -n "/\(['\"]\)[1-9][0-9]\{6,7\}\1/p" /path/to/data.txt
'7654321'
"87654321"

使用Extended Regular Expressions (ERE)

shell> grep -E "(['\"])[1-9][0-9]{6,7}\1" /path/to/data.txt
'7654321'
"87654321"
shell> sed -n -r "/(['\"])[1-9][0-9]{6,7}\1/p" /path/to/data.txt
'7654321'
"87654321"

总结:Grep和Sed同时支持BRE和ERE两种正则,缺省情况下,Grep和Sed使用的都是BRE正则,通过增加命令参数(grep -E / sed -r),Grep和Sed可以支持ERE正则。

BTW:Regular expression From Wikipedia, the free encyclopedia

 


This entry was posted in Technical and tagged Grep, Linux, Regex, Sed, Shell by 老王. Bookmark the permalink.

 

posted on 2012-08-01 20:35  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/08/01/2618999.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值