sed正则表达式匹配,各种括号的转义和不转义

发布时间:2019-01-10 13:46:02 阅读:1099次

[root@web_docker dev.www]# cat access.log |head
[3/Dec/2018:00:03:47 +0800] 49.221.18.66 - 184  "GET http://www.test.com/favicon.ico" 200 1624 565 HIT "Mozilla/5.0 (Linux; Android 6.0; Letv X500 Build/DAXCNCU5902812115S; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/49.0.2623.91 Mobile Safari/537.36" "image/x-icon"
[3/Dec/2018:00:03:54 +0800] 49.221.18.66 - 360  'GET http://www.test.com/a/1284562335694173100?qid=empathy' 200 1920 37485 MISS "Mozilla/5.0 (Linux; Android 6.0; Letv X500 Build/DAXCNCU5902812115S; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/49.0.2623.91 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:53 +0800] 42.248.205.162 - 71  "GET http://www.test.com/?qid=s1zhongqing" 200 490 32589 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:56 +0800] 123.125.67.221 - 129  "GET http://www.test.com/a/1584563609908384837?qid=empathy" 200 297 41309 MISS "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:58 +0800] 42.248.205.162 - 170  "GET http://www.test.com/a/1584562315544432752?qid=s1zhongqing" 200 1352 37952 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:20 +0800] 106.114.48.70 - 170  "GET http://www.test.com/a/1384562314731997990?qid=s1zhongqing" 200 1346 37727 MISS "Mozilla/5.0 (Linux; Android 8.0.0; PRA-AL00 Build/HONORPRA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:22 +0800] 123.125.67.219 - 200  "GET http://www.test.com/a/1284565169684618779" 200 273 39483 MISS "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:26 +0800] 42.248.205.162 - 201  "GET http://www.test.com/a/1584562380768715862?qid=s1zhongqing" 200 1921 40740 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:31 +0800] 42.248.205.162 - 122  "GET http://www.test.com/a/1584562380768715862?qid=s1zhongqing" 200 1921 41689 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:37:11 +0800] 106.47.96.125 - 143  "GET http://www.test.com/?qid=empathy" 200 810 32540 MISS "Mozilla/5.0 (Linux; Android 6.0.1; Redmi 4A Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 Mobile Safari/537.36" "text/html; charset=UTF-8"

[root@web_docker dev.www]# cat access.log | sed 's#\?qid=[0-9a-zA-Z]\+##g'| head
[3/Dec/2018:00:03:47 +0800] 49.221.18.66 - 184  "GET http://www.test.com/favicon.ico" 200 1624 565 HIT "Mozilla/5.0 (Linux; Android 6.0; Letv X500 Build/DAXCNCU5902812115S; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/49.0.2623.91 Mobile Safari/537.36" "image/x-icon"
[3/Dec/2018:00:03:54 +0800] 49.221.18.66 - 360  'GET http://www.test.com/a/1284562335694173100' 200 1920 37485 MISS "Mozilla/5.0 (Linux; Android 6.0; Letv X500 Build/DAXCNCU5902812115S; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/49.0.2623.91 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:53 +0800] 42.248.205.162 - 71  "GET http://www.test.com/" 200 490 32589 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:56 +0800] 123.125.67.221 - 129  "GET http://www.test.com/a/1584563609908384837" 200 297 41309 MISS "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:35:58 +0800] 42.248.205.162 - 170  "GET http://www.test.com/a/1584562315544432752" 200 1352 37952 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:20 +0800] 106.114.48.70 - 170  "GET http://www.test.com/a/1384562314731997990" 200 1346 37727 MISS "Mozilla/5.0 (Linux; Android 8.0.0; PRA-AL00 Build/HONORPRA-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/68.0.3440.91 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:22 +0800] 123.125.67.219 - 200  "GET http://www.test.com/a/1284565169684618779" 200 273 39483 MISS "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:26 +0800] 42.248.205.162 - 201  "GET http://www.test.com/a/1584562380768715862" 200 1921 40740 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:36:31 +0800] 42.248.205.162 - 122  "GET http://www.test.com/a/1584562380768715862" 200 1921 41689 MISS "Mozilla/5.0 (Linux; Android 8.1.0; Redmi Note 5 Build/OPM1.171019.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/62.0.3202.84 Mobile Safari/537.36" "text/html; charset=UTF-8"
[3/Dec/2018:00:37:11 +0800] 106.47.96.125 - 143  "GET http://www.test.com/" 200 810 32540 MISS "Mozilla/5.0 (Linux; Android 6.0.1; Redmi 4A Build/MMB29M; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 Mobile Safari/537.36" "text/html; charset=UTF-8"

https://blog.csdn.net/luo617/article/details/82225260

为了方便自己检索 
转自:https://blog.csdn.net/zl87758539/article/details/77481679?locationNum=10&fps=1
a='>VFG037176(gb|YP_001844723) (plc) phospholipase C [Phospholipase C (VF0470)] [Acinetobacter baumannii ACICU]'
echo $a  | sed 's/^>\(VFG[0-9]*\)(\(.*[0-9]\{1,15\}\)) (\([^ ,]\{1,20\}\)\?) .* \[.*(\([A-Z]*[0-9]*\))\] \[\(.*\)\]/\1\t\2\t\3\t\4\t\5/g'
1.匹配使用的括号 转义 \( \) \{ 
2.第一个匹配用 \1 \2

[ ] 需要匹配的时候,需要转义(这个是叛徒)
echo "[ ]"|sed 's/\[.*\]/aaa/g'

( ) 需要匹配的时候,不要转义
echo "( )"|sed 's/( )/c/g'

{ } 需要匹配的时候,不要转义
echo "{ }"|sed 's/{ }/c/g'

当需要匹配数字,字母等使用中括号时候[]不要转义
但使用{}大括号作为特殊字符时候,{}需要转义。
echo "333"|sed 's/[0-9]\{3\}/ccc/g'

当需要适配符,需要使用\1来替换正则表达式的对应参数时:()需要转义
不能写(regrexxxx),要写\(regrexxxx\)
echo "{1234567}"|sed 's/{\([0-9]*\)}/\1/g'

与此对应的还有 +和*
在做为特别字符时候+必须转义为
\+才有效,
而*则不需要。

举个例子:
echo "ccc"| sed 's/c*/aaa/g'#正确
echo "ccc"| sed 's/c\*/aaa/g'#错误
echo "ccc"| sed 's/c+/aaa/g'#错误
echo "ccc"| sed 's/c\+/aaa/g'#正确

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:sed正则表达式匹配,各种括号的转义和不转义 出自老鄢博客 | 欢迎分享