-2

I have the following string:

{"attributes":{"name":"James Franco","sectors":{"all":{"name":"all","rank":1},"second":{"name":"Actor","rank":1},"third":{"name":"all","rank":1},"team":"BR",{"name":"all","rank":1},"rank":1,"trending":101,rank":1,"BPR":6.50,"SPR":6.10}}

I want to only extract the 'name', 'BPR' & 'SPR' and their respective values from the one line, and output all three to one line, in a new file.

Can anyone tell me please, how to do this using either gnu sed, cut etc ?

LLB3000
  • 45
  • 2
  • 8

1 Answers1

0

With gnu awk:

$ awk -F: -v RS="," '{gsub(/[}]/,"");gsub("\n","")} \
NR==1{printf "%s:%s%s",$2,$3,RT} $1=="\"BPR\"" || $1=="\"SPR\""{printf "%s%s",$0,RT} \
END{printf "\n"}' file
#Output
"name":"James Franco","BPR":6.50,"SPR":6.10
George Vasiliou
  • 6,130
  • 2
  • 20
  • 27
  • Thanks George. Can you elaborate on what exactly that command is doing, breaking it down for me please? I have a basic grasp of awk, but this looks quite in depth for me to understand! Thanks. – LLB3000 May 31 '17 at 20:28