Fetch OEIS Sequences
May 21, 2019
I wrote a simple pipeline of Unix commands and stored it in a shell script:
$ cat >oeis # oeis sequence -- fetch sequence from oeis.org # write sequence items one per line to stdout # example: to fetch prime numbers: oeis a000040 wget -qO- oeis.org/$1/internal | # retrieve sequence grep "%[STU]" | # delete extra lines sed 's/^.*%[STU] //' | # remove html command awk '{printf "%s", $0}' | # join into one line tr "," "\n" # each on its own line CTRL-D $ chmod +x oeis $ oeis a000040 | head -5 2 3 5 7 11 $ oeis a000040 | tr "\n" "," 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271 $ primes=$(oeis a000040) $ echo $primes 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 $ for p in $(oeis a000040) > do echo "$p \c" > done 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 $ ed r !oeis a000040 202 ,p 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 w primes q You can see the program, but not run it, at https://ideone.com/PI73w2.
Since OEIS offers a nice JSON format, I thought I’d take advantage:
get_seq adds the ‘id:’ prefix to the search term, so only one sequence is fetched. Next step is to add command line switches to control that behaviour, so multiple sequences can be fetched at once.
Here’s a solution in Python.
Example Usage: