sed JSON正则表达式 [英] sed JSON regular expression
问题描述
为这样的假问题道歉,但这是我第一次使用curl
命令,现在我从某个地方得到了该命令以提取以下字符串
Apologize for such dummy question but this is my first time using curl
command and now I got this command from somewhere to extract the following string
{成功":true,结果":1,总计":1,更多":false,偏移量":0,命中":[{路径":"/home/users/Vq7DPVRHzGVK--OTJsHs,"摘录:","名称:" Vq7DPVRHzGVK--OTJsHs,"标题:" Vq7DPVRHzGVK--OTJsHs," lastModified:" 2017-03-03 16:45:46,"创建:" 2017-03-03 16:45:46}]}
{"success":true,"results":1,"total":1,"more":false,"offset":0,"hits":[{"path":"/home/users/Vq7DPVRHzGVK--OTJsHs","excerpt":"","name":"Vq7DPVRHzGVK--OTJsHs","title":"Vq7DPVRHzGVK--OTJsHs","lastModified":"2017-03-03 16:45:46","created":"2017-03-03 16:45:46"}]}
我使用以下脚本将curl
输出传送到sed
:
I pipe the curl
output to sed
with the following script:
sed -e 's/^.*"path":"\([^"]*\)".*$/\1/
结果:
/home/users/Vq7DPVRHzGVK--OTJsHs
/home/users/Vq7DPVRHzGVK--OTJsHs
任何人都可以解释正则表达式在这里的工作方式吗?以及如何仅获得 Vq7DPVRHzGVK--OTJsHs 的结果,而不包括/home/user路径?
Can anyone explain how's the regex work here? and how do I get the result for only Vq7DPVRHzGVK--OTJsHs instead of including the /home/user path?
推荐答案
说明:
s/ ^.*"path":"\([^"]*\)".*$ / \1 /
----------^------------ ---^---
Pattern Replacement string
正则表达式如何工作:
^.* # Match beginning of input string & anything else
"path":" # Up to literal string `"path":"`
\([^"]*\) # Then match slash and match + group anything up to a double quote `"`
".*$ # Match double quote and the rest of input string
通过替换字符串\1
,您正在将整个匹配部分替换为第一个捕获组,该捕获组是路径值双引号之间的所有内容,除了开始斜杠之外.
By replacement string \1
you are replacing whole matching part with first capturing group which is every thing between double quotes of path value except the beginning slash.
您想要的是将捕获组从捕获整个部分更改为最后一部分:
What you want is changing capturing group from capturing whole part to last section:
s/^.*"path":"[^"]*\/\([^"]*\)".*$/\1/
这篇关于sed JSON正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!