Suppose i have this json file,
{
"tags": [
{
"name": "xxx1",
"image_id": "yyy1"
},
{
"name": "xxx2",
"image_id": "yyy2"
}
]
}
I want to parse just the values from key 'name' and save it to another text file example.txt. My desired output in the text file would be
xxx1
xxx2
How do i do this?
CodePudding user response:
Save your data in temp.json and use the below command.
cat temp.json | grep name | cut -d ":" -f 2 | cut -d "," -f 1 >> output.txt
CodePudding user response:
Suggesting gawk
(standard awk
in most linux machines) script.
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
}
running
gawk -f script.awk input.json > output.txt
Or one-line gawk
script
gawk '$2~"name"{print $4}' FS="\"|\":" input.json > output.txt
CodePudding user response:
jq -r '.tags[].name' input.json > output.txt