本文共 1828 字,大约阅读时间需要 6 分钟。
$ echo "bt" |grep 'b[ae]?t‘ --如果没有使用ERE就不会匹配到任何的数据
$ echo "bt" |grep -E 'b[ae]?t' --启用了ERE之后,就可以正确匹配了。 bt 关于BRE,主要有以下的模式 纯文本 特殊字符 锚字符 点字符 字符组 排除字符组 使用区间 特殊字符组 星号--纯文本 对于纯文本来说是大小写敏感的,空格和数字也可以正常匹配,胆识空格不需要转义 比如我们需要匹配 一个字符串出现this的情况,使用This和this就有很大的不同。 ---使用sed来匹配 $ echo "This is a test"|sed -n '/This/p' This is a test echo "This is a test"|sed -n '/this/p' $ --使用awk来匹配 $ echo "This is a test"|awk '/This/{print $0}' This is a test --使用grep来匹配 $ echo "This is a test"|grep ‘This' This is a test $ echo “This is a test”|grep -i ‘THis‘ --忽略大小写 This is a test--特殊字符以下的字符在正则表达式中会被视为特殊字符。 .*[]^$\+?|() 所以不能在文本中单独使用如上的特殊字符,需要加\做一个转义。 --sed来匹配字符\ echo "\ is a special character"|sed -n '/\\/p' \ is a special character --sed来匹配字符$,以下是几种测试的场景,可以对比一下输出结果。
$ echo "The price is $5.00"|sed -n '/$/p'
The price is .00 $ echo "The price is $5.00"|sed -n '/\s/p' The price is .00$ echo "The price is $5.00"
The price is .00 $ echo "The price is \$5.00" The price is $5.00 $ echo 'The price is $5.00' The price is $5.00 $ echo 'The price is $5.00'|sed -n '/\$/p' The price is $5.00 echo "The price is $5.00"|sed -n '/\$/p'对于字符/
$ echo "3 / 2"|sed -n '///p' sed: -e expression #1, char 3: unknown command: `/‘ 出这个错误是因为左斜线不是正则表达式的特殊字符的一部分,但是也需要转义,下面这样就没有问题了。 $ echo "3 / 2"|sed -n '/\//p' 3 / 2 后续部分在第二篇中继续分享。来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/23718752/viewspace-1303289/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/23718752/viewspace-1303289/