Chr*_*rgh 4 regex bash curl sed
我正在研究一个简短的bash脚本来从curl响应中获取JSON元素.
curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false
Run Code Online (Sandbox Code Playgroud)
收益:
[{"id":0,"seq":0,"raw":"?b?t(?)n","rawType":"IPA"},{"id":0,"seq":0,"raw":"?b?t(?)n","rawType":"IPA"}]
Run Code Online (Sandbox Code Playgroud)
我正试图提取"bʌt(ə)n"元素.
虽然我不熟悉正则表达式,但我认为我应该使用这个字符串替换:
/.*"(.*)",/
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行以下命令,但它似乎不起作用:
curl -H "api_key:[API_KEY]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false | sed /.*"(.*)",\1/
Run Code Online (Sandbox Code Playgroud)
我确定有一些事情我做错了,经过几个小时的搜索和阅读正则表达式和bash我没有选择.
我不需要使用sed,我只是在bash命令行中寻找一种快速的方法,这样我就可以在mac上的TextExpander脚本中实现它.
用于STRING : REGEXP从json字符串中提取值:
string=$(curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false)
raw=$(expr "$string" : '.*"raw":"\([^"]*\)"')
echo $raw
Run Code Online (Sandbox Code Playgroud)
见man expr:
STRING : REGEXP
anchored pattern match of REGEXP in STRING
Pattern matches return the string matched between \( and \) or null
Run Code Online (Sandbox Code Playgroud)