zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Cary Lewis <cary.lewis@gmail.com>, zsh-workers@zsh.org
Subject: Re: tee stdin and grep without creating a temp file
Date: Thu, 24 Oct 2013 18:16:27 -0700	[thread overview]
Message-ID: <131024181627.ZM8127@torch.brasslantern.com> (raw)
In-Reply-To: <CAEF1h+Vtag-e_rz6DLe8OWy9h=boxFD69VOT-q5w6+OZ49iVRQ@mail.gmail.com>

[This is really a question of the zsh-users variety, but ...]

On Oct 23,  2:46pm, Cary Lewis wrote:
} 
} I have a situation where I need to grep an stdin input stream, and based on
} whether I find a certain pattern, take appropriate action. The action
} involves processing the stdin stream again.

This is only going to work with a temp file, because your last step
("take appropriate action") depends on two inputs, the result of grep
and the original input stream.  That means the input stream has to be
buffered somewhere until grep gets a hit, and the only way you can do
an arbitrary amount of buffering is to use a temp file.

However, you should be able to run the grep in parallel with creating
the temp file, rather than capturing the entire output in the file
before you begin to grep it.

} I tried to create a fifo with mkfifo, and tee the stdin to the pipe
} and then grep on the output of the tee. This approach doesn't work
} correctly.

For this to work, you'd have to have something reading from the fifo
to keep the buffer from filling up.  It ought to be possible to get
this approach to work by replacing the fifo with an actual file, or to
use a zsh "multio" instead of tee.

    setopt multios # Probably not needed, this is the default
    if print -l some simulated input > bufferfile | grep -q simulated
    then read -E appropriate_action < bufferfile
    fi

Depending on how far back you need to look in the stream once the
grep succeeds, you might consider using e.g. perl for the whole task.
Buffer what you need inside the program that's doing the pattern match,
then have that program branch to the appropriate action.  This would
be particularly effective if you don't need to "look behind" at all,
that is, if it's only a matter of changing where the remainder of the
output is going once the pattern matches.

You could do that in zsh with a "while read ..." loop but that would
be slower than grep or perl on a large input.


      reply	other threads:[~2013-10-25  1:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-23 18:46 Cary Lewis
2013-10-25  1:16 ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=131024181627.ZM8127@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=cary.lewis@gmail.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).