I'm writing a short script to pull the price of bitcoin. The information will be piped into an i3block so that I can see the hourly price on my status bar - here is what I have so far.
#!/bin/bash
price=$(curl -s gbp.rate.sx/btc?T | tail -3 | head -1 | grep -o '[0-9]*' | cut -d ' ' -f1)
echo $price
I curl the bitcoin price (in £) from rate.sx with the T option (text only, no ANSI sequences), select the average price line using head and tail and grep out the numbers. The output looks something like this:
26563 26396 1738 8 6 29
The first number is the average price, with the others being the median, change, etc... As you can see, I pass this through cut with whitespace as the delimiter, but the output is uncut.
I also tried to swap whitespace for ',' to try convert the output to csv, but this failed also.
price=$(curl -s gbp.rate.sx/btc?T | tail -3 | head -1 | grep -o '[0-9]*' | sed 's/ /,/' )
The above also gave me the same output. Why are cut
and sed
struggling to recognise the whitespace?
curl -s gbp.rate.sx/btc?T | tail -3 | head -1 | grep -o '[0-9]*'
you will notice thatgrep -o '[0-9]*'
outputs each matching sequence of digits on a separate line – steeldriver Jan 17 '21 at 13:39curl … | LC_ALL=C grep -Po '^avg:.*?\K[[0-9]+'
I can explain this if it really does match what you want. – Chris Davies Jan 17 '21 at 13:41price=$(curl -s gbp.rate.sx/btc?T | tail -3 | head -1 | grep -o '[0-9]*' | cut -d$'\n' -f1)
– Deez Jan 17 '21 at 13:54cut
command in your last comment just the same ashead -1
again? – Kusalananda Jan 17 '21 at 15:18