Path: utzoo!utgpu!water!watmath!clyde!att!pacbell!ames!pasteur!agate!ig!uwmcsd1!marque!uunet!mcvax!philmds!leo From: leo@philmds.UUCP (Leo de Wit) Newsgroups: comp.unix.wizards Subject: Re: grep replacement Message-ID: <499@philmds.UUCP> Date: 9 Jun 88 10:02:06 GMT References: <136@rubmez.UUCP> <449@happym.UUCP> Reply-To: leo@philmds.UUCP (L.J.M. de Wit) Organization: Philips I&E DTS Eindhoven Lines: 42 In article <449@happym.UUCP> kent@happym.UUCP (Kent Forschmiedt) writes: >In article <136@rubmez.UUCP> frei@rubmez.UUCP (Matthias Frei ) writes: >>I want following flags: >> >> - d divert the file >> "matches" to stdout >> "nomatches" to stderr >> -r exchange stdout and stderr, if -d is given >I second the vote - just today I did one of these: > >grep $PATTERN file > afile >grep -v $PATTERN file > anotherfile > >Note, however, that -v will serve for the suggested -r. >>Will you post Your new grep to the net ? (I hope so) >From alice.UUCP?? Ha ha! That's Bell Labs! It will be in V10 >Unix, and none of us humans will see it until sysVr6, and only then >if we are lucky!! You are lucky, because here's your_new_grep: ---------------------- S T A R T H E R E --------------- #!/bin/sh # Usage: yngrep pattern matches nomatches [file ...] case $# in 0|1|2) echo "Usage: $0 [file ...]"; exit;; *) pattern=$1 matches=$2 nomatches=$3; shift; shift; shift;; esac exec sed -n -e " /$pattern/w $matches /$pattern/!w $nomatches" $* ---------------------- S T O P H E R E --------------- Use the p command of sed to write to stdout. I don't know how to write to the stderr from within sed. Don't think exec 2>outfile beforehand works, because sed does not open for append. But you could use w /dev/tty, that's often what you want for stderr anyway 8-). Hope it works right away, didn't test it. Leo.