Bash – Best way to partially step through a 25k string

bashscriptingshell-script

I have a 25k characters.

I wish to write a script to print (printf is most portable I'm told) an arbitrary number of characters; stepping through them in order.

say:

command number

Where number can be any value of 1-25000, and get that output.

I would prefer to not have the data in a separate file (the easiest solution?), and I'd prefer to use only POSIX shell commands (to make the script as portable as possible: I'm aware awk or perl could whack this out simply).

Should I store this data in a variable? Or run my complete printf through a cut command (cut -c -$1)? Or is there another (better?) solution? Why might I choose one option over another option?

What other problems/caveats am I over-looking?

Best Answer

Have you considered the dd command? It lets you skip any number of bytes, then output any number of bytes.

dd if=infilename bs=1 skip=sk count=ct 2>/dev/null

dd, input file name, block size 1, skip first sk bytes of input file, then copy ct bytes to stdout (or specify a file with of=name). Redirect error messages to avoid the status messages it usually prints at the end.