grep的grouping

存在文本config.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"port": 8000,

"userName": "*****",
"password": "*******",
"desDir": "//192.168.1.54/share",
"tarDir": "/share",

"copysrc": "Z:\\sonia",
"copydst": "D:\\share",

"WEB_SERVE": "11.54.34.130:8000",
"GENUI_DIR": "/home/*****/src",
"PACK_DIR": "/home/*****/src"
}

使用grep匹配其中WEB_SERVE的值

1
2
3
4
5
cat config.json |grep WEB_SERVE | grep -oP '\"(\d.*?)\"'
# "11.54.34.130:8000"
cat config.json |grep WEB_SERVE | grep -oP '(?<=")\d.*?(?=")'
# 11.54.34.130:8000

通过本次测试发现,grep内的grouping命令并不都能像python re模块中直接匹配组的内容,想要直接获取文本匹配固定内容可通过断言获得

1
2
以双引号开头:(?<=")  
以双引号结尾:(?=")