On Fri, 13 Jun 1997 11:18:02 -0400,
Stan Ryckman <stanr(_at_)sunspot(_dot_)tiac(_dot_)net> wrote:
FYI, here's an awk version:
Yeah, but it's slower (^:
$ cat /usr/dict/words >/dev/null
$ time perl -0777 -ne 'exit 1 unless (/foo/ and /bar/)' /usr/dict/words
real 0.1 user 0.0 sys 0.0
$ time awk '/foo/{foo=1} /bar/{bar=1} END{exit (foo+bar!=2)}' /usr/dict/words
real 0.3 user 0.2 sys 0.0
It doesn't seem to matter whether the strings are found or not
(obviously, but I thought I'd mention that I tested it).
Perl is probably slower to load than awk so this might make a bigger
difference in the present situation. Still, I'd love to see a sed
solution rather than either of these. (It can't be impossible.)
I've almost got a solution, but I can't figure out how to produce a
run-time error in sed (to get an error code when it fails). Maybe it's
impossible. This one will print one or more spurious lines if it
doesn't match; you can pipe it into grep '$' to convert that to an error
code, f'rinstance.
But it's almost as quick as the Perl one, and the program image should
be significantly smaller than you get with Perl:
$ time sed -ne '/foo/H' -e '/bar/H' \
-e '${' -e 'g' -e '/foo/!p' -e '/bar/!p' -e '}' /usr/dict/words
real 0.1 user 0.1 sys 0.0
$ time sed -ne '/foo/H' -e '/bar/H' \
-e '${' -e 'g' -e '/foo/!p' -e '/bar/!p' -e '}' /usr/dict/words |
grep '$'
real 0.1 user 0.1 sys 0.0
/* era */
Turing machine programmers anonymous. The world should run on Perl.
--
Defin-i-t-e-ly. Sep-a-r-a-te. Gram-m-a-r. <http://www.iki.fi/~era/>
* Enjoy receiving spam? Register at <http://www.iki.fi/~era/spam.html>