0

I have a text file whose contents looks like this:

{"result":"true","last":0.02094,"high":0.0213,"low":0.01682,"avg":0.01871,"sell":0.02094,"buy":0.02054,"vol_ltc":5145.8262,"vol_btc":96.29887,"rate_change_percentage":"23.47"}`

(This is data from an API)

I want it formatted so my program can read it, but I need to extract the numbers because some of the exchanges are formatted differently by the API, for example, this:

{"result":"true","last":"0.00000070","high":"0.00000073","low":"0.00000069","avg":"0.00000071","sell":"0.00000071","buy":"0.00000069","vol_doge":71072621.657,"vol_btc":50.64272,"rate_change_percentage":"0.00"}

(Difference: quotes around price values)
And I need something that looks like this:
(Using the first one)

0.02094
0.0213
0.01682
0.01871
0.02094
0.02054
5145.8262
96.29887
23.47

Alternatively something like this (again, using the first one)

last
0.02094
high
0.0213
low
0.01682
avg
0.01871
sell
0.02094
buy
0.02054
vol_ltc
5145.8262
vol_btc
96.29887
rate_change_percentage
23.47

If you need more examples of strings provided from the API, go to http://data.bter.com/api#tickers

0

2 Answers 2

3

As the API returns JSON objects, I'd use a JSON parser for it, combined with sed and grep.

I've decided for jq, which is contained in the Ubuntu repo's, so it could be installed using sudo apt-get install jq; I don't know what distro you are using, so you'll have to find that by yourself if you want to use this solution.

jq ".[]" <filename> | sed s/\"//g | grep -P '^\d+(\.\d+)$'

Now, let me split this up:

  1. jq ".[]" <filename> will print the value for each key in the map. This probably doesn't work with nested maps, though.
  2. sed s/\"//g will strip the list of any quotation marks.
  3. grep -P '^\d+(\.\d+)?$' will return those lines that consist of integer or decimal numbers only.

This command chain will print the result to stdout. To get it into a file, simply pipe the output into it:

jq ".[]" <filename> | sed s/\"//g | grep -P '^\d+(\.\d+)$' > <other_filename>
5
  • you can use the tonumber function: jq '.[] | tonumber'. That's all:) Jul 6, 2015 at 21:43
  • That will output in scientific notation...
    – s3lph
    Jul 6, 2015 at 21:47
  • Good thing brew (homebrew) has lots of stuff that are in Ubuntu repos, because I'm on mac :P. There was a small syntax error in the last line of code you provided, it should have been jq ".[]" <filename> | sed s/\"//g | grep -P '^\d+(\.\d+)$' > <other_filename> (at least for me.) Thanks for posting this! Jul 6, 2015 at 22:08
  • @the_Seppi When piping it to a file, it's missing the last value (percentage change). Jul 6, 2015 at 23:16
  • Only when piping, but not when printing to stdout?
    – s3lph
    Jul 7, 2015 at 10:24
0

here is a perl one-liner:

perl -pe 's/.*?(\d+\.\d+)/$1 . "\n"/eg' file1 > file2
2
  • It leaves garbage at the end: "}
    – yaegashi
    Jul 6, 2015 at 23:11
  • ok, try this one: perl -pe 's/.*?(\d+\.\d+)/$1 . "\n"/eg' file1 | egrep '^[0-9]' > file2 Jul 6, 2015 at 23:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .