假設我有這個 json 檔案,
{
"tags": [
{
"name": "xxx1",
"image_id": "yyy1"
},
{
"name": "xxx2",
"image_id": "yyy2"
}
]
}
我只想決議鍵“name”中的值并將其保存到另一個文本檔案 example.txt。我在文本檔案中想要的輸出是
xxx1
xxx2
我該怎么做呢?
uj5u.com熱心網友回復:
將您的資料保存在 temp.json 中并使用以下命令。
cat temp.json | grep name | cut -d ":" -f 2 | cut -d "," -f 1 >> output.txt
uj5u.com熱心網友回復:
建議gawk(awk大多數 linux 機器中的標準)腳本。
script.awk
BEGIN { # preprocessing input file
FS="\"|\":"; # set fields seperator to " and ":
}
$2 ~ "name" { # if 2nd field match "name" in current line
print $4; # print 4th field
}
跑步
gawk -f script.awk input.json > output.txt
或單行gawk腳本
gawk '$2~"name"{print $4}' FS="\"|\":" input.json > output.txt
uj5u.com熱心網友回復:
jq -r '.tags[].name' input.json > output.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485607.html
