免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1932 | 回复: 2
打印 上一主题 下一主题

[文本处理] sed -r 扩展正则表达式 --举例说明 [复制链接]

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-05-05 09:57 |只看该作者 |倒序浏览
本帖最后由 1cpuer 于 2018-05-05 10:18 编辑

r 为何物
  -r, --regexp-extended
                 在脚本中使用扩展正则表达式

--举例说明
2,3个 优加
# [ /home/soio/1bs/awks ] {2018-05-05 10:14:00}
: 1525486440:0;➜  echo "\!@#()$%^" | sed 's/(/ar/'
!@#ar)$%^


# [ /home/soio/1bs/awks ] {2018-05-05 10:15:15}
: 1525486515:0;➜  echo "\!@#()$%^" | sed -r 's/\(/ar/'
!@#ar)$%^


# [ /home/soio/1bs/awks ] {2018-05-05 10:15:25}
: 1525486525:0;➜  echo "\!@#()$%^" | sed -r 's/(/ar/'
sed: -e 表达式 #1, 字符 7: 不匹配的 ( 或 \(↵



论坛徽章:
39
双子座
日期:2014-08-06 17:37:19极客徽章
日期:2016-12-07 14:03:402017金鸡报晓
日期:2017-01-10 15:13:292017金鸡报晓
日期:2017-02-08 10:39:4215-16赛季CBA联赛之新疆
日期:2017-03-24 16:36:1915-16赛季CBA联赛之江苏
日期:2017-04-26 17:19:08黑曼巴
日期:2018-03-07 18:56:5615-16赛季CBA联赛之八一
日期:2018-03-09 10:44:1015-16赛季CBA联赛之江苏
日期:2018-03-12 15:12:1915-16赛季CBA联赛之青岛
日期:2018-03-16 09:13:0515-16赛季CBA联赛之山东
日期:2018-04-27 18:23:0515-16赛季CBA联赛之新疆
日期:2018-05-04 11:29:30
2 [报告]
发表于 2018-05-05 11:15 |只看该作者
   Basic vs Extended Regular Expressions
       In  basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the
       backslashed versions \?, \+, \{, \|, \(, and \).

       Traditional egrep did not support the { meta-character, and some egrep  implementations  support  \{  instead,  so
       portable scripts should avoid { in grep -E patterns and should use [{] to match a literal {.

       GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an
       invalid interval specification.  For example, the command grep -E ’{1’ searches for the  two-character  string  {1
       instead  of  reporting  a syntax error in the regular expression.  POSIX allows this behavior as an extension, but
       portable scripts should avoid it.

论坛徽章:
6
程序设计版块每日发帖之星
日期:2016-05-11 06:20:00操作系统版块每日发帖之星
日期:2016-05-12 06:20:00每日论坛发贴之星
日期:2016-05-12 06:20:00操作系统版块每日发帖之星
日期:2016-05-14 06:20:00程序设计版块每日发帖之星
日期:2016-05-31 06:20:00每日论坛发贴之星
日期:2016-05-31 06:20:00
3 [报告]
发表于 2018-05-05 17:14 |只看该作者
回复 2# o枫叶o飘零

# [ /home/soio/1bs/awks ] {2018-05-05 17:07:31}
: 1525511251:0;➜  echo "123 abc 24_cd" | grep  '[[:alnum:]]'  
123 abc 24_cd


# [ /home/soio/1bs/awks ] {2018-05-05 17:09:51}
: 1525511391:0;➜  echo "123 abc 24_cd" | grep  '[[:alnum:]_]'
123 abc 24_cd


# [ /home/soio/1bs/awks ] {2018-05-05 17:09:54}
: 1525511394:0;➜  echo "123 abc 24_cd" | grep  '\w'           
123 abc 24_cd


# [ /home/soio/1bs/awks ] {2018-05-05 17:10:02}
: 1525511402:0;➜  echo "123 abc 24_cd" | grep  '[^\w_]'      
123 abc 24_cd

5).正则表达式的扩展
额外的GNU正则表达式运算符
运算符
含义
\w:
任何单词组成字符,相当于[[:alnum:]_]
\W:
任何非单词组成字符,相当于[^[:alnum:]_]      
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP