0

so, I have the example code here:

#!/bin/bash
clear

curl -s  https://www.cnbcindonesia.com/market-data/currencies/IDR=/USD-IDR |
html2text |
sed -n '/USD\/IDR/,$p' |
sed -n '/Last updated/q;p' |
tail -n-1 |
head -c+6 && printf "\n"

exit 0

this should print out some number range 14000~15000

lets start from the very basic one, what I have to do in order to print result + 1 ? so if the printout is 14000 and increment it to 1 become 14001. I suppose the result of the html2text is not calculatable since it should be something like string output not integer.

the more advance thing i want to know is how to calculate the result of 2 curl results?

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
CuriousNewbie
  • 319
  • 4
  • 13

1 Answers1

2

What I would do, + :

$ num=$(xidel -se '//div[@class="mark_val"]/span[1]/text()' 'https://url')
$ num=$((${num//,/}+1)) # num was 14050
$ echo $num 

Output

14051

 Explanations

$((...))

is an arithmetic substitution. After doing the arithmetic, the whole thing is replaced by the value of the expression. See http://mywiki.wooledge.org/ArithmeticExpression

Command Substitution: "$(cmd "foo bar")" causes the command 'cmd' to be executed with the argument 'foo bar' and "$(..)" will be replaced by the output. See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution

Bonus

You can compute directly in , thanks Reino using syntax :

$ xidel -s <url> e 'replace(//div[@class="mark_val"]/span[1],",","") + 1' 

And to do addition arithmetic of 2 values :

$ xidel -s <url> -e '
    let $num:=replace(//div[@class="mark_val"]/span[1],",","")
    return $num + $num
'
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
  • Hi Gilles, thank you for introducing me to xidel, it's much easier now to do simple web scraping with xpath capability tool. however, when i trying out these command, I got an error in terminal. it says `test.sh: 7: test.sh: Bad substitution` , i saved the script as test.sh btw. – CuriousNewbie Jun 13 '20 at 02:36
  • 1
    Can you run this with `bash script.sh` ? It's required. Or replace `${num//,/}` bash parameter expansion by `tr` or `sed` – Gilles Quénot Jun 13 '20 at 02:41
  • ah! i was using `sh test.sh` , i thought it wont cause any problem. cool now it works. – CuriousNewbie Jun 13 '20 at 02:45
  • so If i about to combine 2 links, it would be like this? `$ num=$(xidel -se '//div[@class="mark_val"]/span[1]/text()' 'https://url') $ num=$((${num//,/}+1)) # num was 14050 $ nam=$(xidel -se '//div[@class="mark_val"]/span[1]/text()' 'https://url') $ nam=$((${nam//,/}+1)) # nam was 14050 $ echo $num + $nam` ? – CuriousNewbie Jun 13 '20 at 02:47
  • 1
    `echo $((num+nam))` – Gilles Quénot Jun 13 '20 at 02:54
  • oh right, cool now all my problem solved, thank you very much – CuriousNewbie Jun 13 '20 at 02:55
  • @GillesQuenot `xidel -s -e 'replace(//div[@class="mark_val"]/span[1],",","") + 1'` – Reino Jun 13 '20 at 12:37
  • @Reino: Are you a dev from `xidel` ? :) – Gilles Quénot Jun 13 '20 at 12:39
  • 1
    @AhiungLim If all you want is 14050+14050, then there's no need for multiple `xidel` calls: `xidel -s -e 'let $num:=replace(//div[@class="mark_val"]/span[1],",","") return $num + $num'` – Reino Jun 13 '20 at 12:40
  • 1
    @GillesQuenot No, I'm not. Just an active user. – Reino Jun 13 '20 at 12:42
  • Added this as 'Bonus' – Gilles Quénot Jun 13 '20 at 12:45