2013-02-23 57 views
2

玩家外壳脚本:用于抓取数据并减去从游戏A和B

wget --output-document=- http://runescape.com/title.ws 2>/dev/null \ 
    | grep PlayerCount \ 
    | head -1l \ 
    | sed 's/^[^>]*>//' \ 
    | sed "s/currently.*$/$(date '+%r %b %d %Y')/" \ 
    | cut -d">" -f 3,4 \ 
    | sed 's/<\/span>//' 

输出:111,048 people 10:43:54 PM Feb 22 2013

玩家从游戏B:

wget --output-document=- http://oldschool.runescape.com/ 2>/dev/null | grep "people playing" 

输出:There are currently 42823 people playing!

我想知道有多少玩A游戏,但我不太确定如何玩这个游戏数字你这两个输出的获得,并以同样的格式是这样减去他们,将他们输出:

`111,048 people 10:43:54 PM Feb 22 2013` 
+1

一旦你把数字变成变量,用'(($ a - $ b))'来减去它们。 – Barmar 2013-02-23 04:04:56

+0

谢谢。只是不知道如何修剪和存储数字,然后再次显示它:( – Aaron 2013-02-23 04:06:29

+0

你说你被困在数学部分。它看起来像我有一个如何使用sed拉开字符串的句柄。 – Barmar 2013-02-23 04:08:31

回答

1
#!/bin/sh 

URL1=http://runescape.com/title.ws 
tot=`wget -qO- $URL1 | grep -i PlayerCount | cut -d\> -f4 | cut -d\< -f1 | sed -e's/,//'` 
URL2=http://oldschool.runescape.com 
b=`wget -qO- $URL2| grep "people playing" | awk '{print $4}'` 
a=`expr $tot - $b` 
echo "$a people `date '+%r %b %d %Y'`" 

...如果你想逗号做这些行添加到脚本...

export LC_ALL=en_US.UTF-8 
a_with_comma=`echo $a | awk "{printf \"%'d\n\", \\$1}"` 
echo "$a_with_comma people `date '+%r %b %d %Y'`" 
+1

哇,你让它看起来很容易。有没有办法在数字结果中添加逗号?我是强迫症:( – Aaron 2013-02-23 04:47:19

+1

我不认为有一种简单的方法来给数字添加逗号,你必须写一个循环,以3位数字的组来分开数字 – Barmar 2013-02-23 05:13:47

+0

我已经更新了我对逗号的回答 – 2013-02-23 05:23:57

3
total=$(wget --output-document=- http://runescape.com/title.ws 2>/dev/null | 
     sed -n '/PlayerCount/{s/^[^0-9]*<span>\([0-9,]*\).*/\1/;s/,//g;p;q;}') 
gameb=$(wget --output-document=- http://oldschool.runescape.com/ 2>/dev/null | 
     sed -n '/people playing/{s/There are currently \([0-9]*\) people playing!/\1/;p;q;}') 
gamea=$(($total - $gameb)) 
+0

嘿,谢谢你,你怎么添加一个逗号?数字? – Aaron 2013-02-23 05:08:36