-6

I have this very long line of info in 1 file, and i want to extract it, so that the end result look like this output.txt ? Please help !

My input looks like:

[{"city":"london","first name":"peter","last name":"hansen","age":"40"},
{"city":"new york","first name":"celine","last name":"parker","age":"36"]

Output.txt

peter (40)
celine (36)
John Goofy
  • 1,190
  • 1
  • 9
  • 19

2 Answers2

1

If it is proper json, an alternative is to use jq:

$ cat test.json
[{"city":"london","first name":"peter","last name":"hansen","age":"40"},
  {"city":"new york","first name":"celine","last name":"parker","age":"36"}]

$ jq -r '.[] | "\(."first name") (\(.age))"' test.json
peter (40)
celine (36)
MauricioRobayo
  • 1,916
  • 20
  • 22
0

Assuming that the input string is stored in a file input.txt, i.e.

echo '[{"city":"london","first name":"peter","last name":"hansen","age":"40"},{"city":"new york","first name":"celine","last name":"parker","age":"36"}]' >input.txt

you can get the result by

ruby -e "puts $(<input.txt).map {|x| %(#{x[:'first name']} (#{x[:age]}))}"

(This uses the fact that the input happens to be a syntactically valid Ruby expression, denoting an array of two hashes).

user1934428
  • 15,702
  • 7
  • 33
  • 75