I'm looking for a program to use an external program to filter the lines of a stream - pretty much a version of grep that, for each line, prints or suppresses it based on whether the specified program exits with a zero exit code (like find's -exec option).
I know that I can do this in shell by using a loop and a subshell:
some-program |(while read line; do
if predicate "$line"; then
echo "$line"
fi
done)
What I'm wondering is if there's a program floating around that will let me make this simpler:
some-program |filter predicate
# want negation as well
some-program |filter ! predicate
One would think that an enhanced Sed might support this, asking "does it pass on the pattern space?", but GNU Sed does not seem to have such a facility.
Is there such a program somewhere that I haven't found, or do I just need to do it in shell (or perl)?
predicateis a shell command? Are you sure you want to run a new process for every line in the file? – alex Jun 22 '11 at 16:32predicateis a shell command, and running a new process for each line is what I'm looking for. Inefficient, but it for one-offs it'd be convenient. I'm actually writing a full Perl script now for the immediate task, but having such a program handy for future reference would still be useful. – Michael Ekstrand Jun 22 '11 at 16:50evalbuilt-in. – alex Jun 22 '11 at 18:27some-program | while ...; do ...; donejust fine. – Teddy Jun 22 '11 at 19:40