[Tfug] rereading stdin

Robert Hunter bobjim.hunter at gmail.com
Fri Feb 17 20:36:02 MST 2006


On 2/17/06, Claude Rubinson <rubinson at u.arizona.edu> wrote:
> I'm wondering how people handle situations in which they need to step
> through stdin more than once.  As an example, lets say that I want to
> compute some deviation statistics: x - mean(x).  The formula requires
> that I step through the input twice, once to compute mean(x) and once
> to compute x - mean(x) for every x.
>
> If I'm working in a higher-level language (such as Python), this isn't
> an issue as I'll have already read the input into an object of some
> sort.  Where it rears its head is when I'm working in shell (as is my
> preference).  I can read the input into variables (or, more likely,
> arrays) or write it first to a temp file (possibly using tee).  I tend
> to prefer the second solution (with tee) but both feel kludgey to me.

There is no way to "rewind" I/O.  For example, imagine the humor of
rewinding stdin by "pushing" characters back out of your keyboard. ;-)
 As you say, if you could parallelize your algorithm then you would
not have this problem, but since you cannot then the only other option
is to store it somewhere so you can make a second pass through it. 
The way you store the data is left to your creativity and the
capability of your tools.  You could redirect it to a file, or store
it in a shell variable or Python object.  You could possibly even get
the OS to do it for you by using pipes.

--
Rob


More information about the tfug mailing list