友 需要从有效负载文件中提取名称。我的有效负载文件包含
"mobileAppTunnel": "disabled","name": "t1",
我只需要提取名称为t1。目前它的t1但它可以是我不知道的任何东西,它可以是10个字母的单词或字母后跟数字。 请帮忙
答案 0 :(得分:2)
关注awk
可能对您有帮助。
awk -F'[, | ]' '{for(i=1;i<=NF;i++){gsub(/"|:/,"",$i);if($i=="name"){gsub(/"|:/,"",$(i+1));print $(i+1)}}}' Input_file
解决方案第二: awk
的简短版本,与第一解决方案采用不同的方法。
awk -v RS="[ |,]" -v FS="" '/^"name":$/{getline;gsub(/"/,"");print}' Input_file
第2项解决方案的说明:
awk -v RS="[ |,]" -v FS="" ' ##Mentioning that record separator as either space or comma, then setting field separator to NULL here for all the lines.
/^"name":$/{ ##Searching for a line which starts from "name": if yes then do following:
getline; ##Using awk out of the box utility getline to take cursor to next line now.
gsub(/"/,""); ##Using gsub global substitution to substitute " with NULL in current line all of its occurrences.
print} ##Printing the current edited/non-edited line now.
' Input_file ##Mentioning the Input_file name here.
答案 1 :(得分:1)
应该是一个相当简单的正则表达式:
<强>在强>:
"mobileAppTunnel": "disabled"
"name": "t1"
<强>正则表达式强>:
/^\s*"name"\s*"(.+)"$/
<强>输出:强>
t1
答案 2 :(得分:1)
$ cat file.json
{ "mobileAppTunnel": "disabled","name": "t1" }
$ jq -r .name file.json
t1