简体   繁体   English

web 中的算术在 shell 中刮擦

[英]Arithmetic in web scraping in a shell

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这应该打印出一些数字范围 14000~15000

lets start from the very basic one, what I have to do in order to print result + 1 ?让我们从最基本的开始,为了打印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.因此,如果打印输出为 14000 并将其增加到 1 变为 14001。我认为 html2text 的结果是不可计算的,因为它应该类似于字符串 output 而不是 integer。

the more advance thing i want to know is how to calculate the result of 2 curl results?我想知道的更高级的是如何计算 2 个 curl 结果的结果?

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 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/ArithmeticExpressionhttp://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 .命令替换: "$(cmd "foo bar")"导致使用参数 'foo bar' 执行命令 'cmd' 并且 "$(..)" 将被output替换。 See http://mywiki.wooledge.org/BashFAQ/002 and http://mywiki.wooledge.org/CommandSubstitution请参阅http://mywiki.wooledge.org/BashFAQ/002http://mywiki.wooledge.org/CommandSubstitution

Bonus奖金

You can compute directly in , thanks Reino using syntax:您可以直接在中计算,感谢Reino使用语法:

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

And to do addition arithmetic of 2 values:并做 2 个值的加法运算:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM