[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Use stdin without tempfile

From: Bob Proulx
Subject: Re: [Help-bash] Use stdin without tempfile
Date: Sun, 1 Dec 2013 20:11:13 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Peng Yu wrote:
> I want to process stdin in the way that the first 10 lines are
> processed by prog1, and the remaining lines are processed by prog2.

> Matthew Cengia wrote:
> > bash$ printf "%s\n" {1..30} > file.txt
> > bash$ cat
> 10 is just an example. When 10 becomes a very large number or the
> input is very long, I guess this is not very efficient? Is there a
> more efficient solution?

Okay.  How about this?  All as described for the previous test case.
But instead of reading in a shell loop use a compiled program such as
sed to count the lines.  (Or awk.)

  sed 10q | prog1
  sed 10q | prog2

This assumes that the problem is the small number of fixed lines as
you stated in your posting.

If you don't have what you asked for in your original posting and you
only want to handle those lines and characters once then it is only
important that your program reads only the lines that it should read
and no more.  Your example of 'cat' reads all lines until EOF.  Modify
your programs to read only what is needed and to exit afterward.  If
you used "sed 10q" instead of cat then the remaining input would be
available for the next program.

  function prog1 {
    sed 10q
  function prog2 {
    sed 10q


reply via email to

[Prev in Thread] Current Thread [Next in Thread]