Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Bash script to crawl the OEIS database, and get the number of entries for each integer. See http://swarminglogic.com/articles/2013_08_oeis for more information.
#!/bin/bash
[[ ! "$1" == "-" && ! "$1" == "+" ]] && echo "Invalid sign paramter [-|+]" && exit;
[[ "$1" == "-" ]] && suff="neg";
[[ "$1" == "+" ]] && suff="pos";
cntfile='cntfile_'$suff
[[ ! -e $cntfile ]] && echo '0' > $cntfile;
file='values_'$suff
last=`cat $cntfile`
sign="$1"
function getnfreq() {
v=`curl --silent "http://oeis.org/search?q=$1" \
| grep -P '\d result(s|\s)' \
| head -n1 \
| sed 's/.*Found\s//g' \
| sed 's/.*of\s//g' \
| sed 's/\s.*//g'`
[[ ! $v ]] && echo 0 || echo $v
}
cnt=0
while [[ true ]]
do
((++cnt))
if [[ $cnt%100 -eq 0 ]] ; then notify-send "OEIS: $last (-)" ; fi
init0=$last
init1=$((last+1))
init2=$((last+2))
init3=$((last+3))
init4=$((last+4))
init5=$((last+5))
init6=$((last+6))
init7=$((last+7))
init8=$((last+8))
init9=$((last+9))
(getnfreq $sign$init0 > ./tmpval-0) && echo -n ' [0]' &
(getnfreq $sign$init1 > ./tmpval-1) && echo -n ' [1]' &
(getnfreq $sign$init2 > ./tmpval-2) && echo -n ' [2]' &
(getnfreq $sign$init3 > ./tmpval-3) && echo -n ' [3]' &
(getnfreq $sign$init4 > ./tmpval-4) && echo -n ' [4]' &
(getnfreq $sign$init5 > ./tmpval-5) && echo -n ' [5]' &
(getnfreq $sign$init6 > ./tmpval-6) && echo -n ' [6]' &
(getnfreq $sign$init7 > ./tmpval-7) && echo -n ' [7]' &
(getnfreq $sign$init8 > ./tmpval-8) && echo -n ' [8]' &
(getnfreq $sign$init9 > ./tmpval-9) && echo -n ' [9]' &
wait $(jobs -p)
# I.e. make sure no other jobs are running before starting
echo "";
echo $init0 `cat ./tmpval-0` >> $file
echo $init1 `cat ./tmpval-1` >> $file
echo $init2 `cat ./tmpval-2` >> $file
echo $init3 `cat ./tmpval-3` >> $file
echo $init4 `cat ./tmpval-4` >> $file
echo $init5 `cat ./tmpval-5` >> $file
echo $init6 `cat ./tmpval-6` >> $file
echo $init7 `cat ./tmpval-7` >> $file
echo $init8 `cat ./tmpval-8` >> $file
echo $init9 `cat ./tmpval-9` >> $file
echo $sign$init0 `cat ./tmpval-0`
echo $sign$init1 `cat ./tmpval-1`
echo $sign$init2 `cat ./tmpval-2`
echo $sign$init3 `cat ./tmpval-3`
echo $sign$init4 `cat ./tmpval-4`
echo $sign$init5 `cat ./tmpval-5`
echo $sign$init6 `cat ./tmpval-6`
echo $sign$init7 `cat ./tmpval-7`
echo $sign$init8 `cat ./tmpval-8`
echo $sign$init9 `cat ./tmpval-9`
rm tmpval-*;
last=$((init9 + 1))
echo $last > $cntfile
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment