grep replacement

Leo de Wit leo at philmds.UUCP
Thu Jun 9 20:02:06 AEST 1988


In article <449 at happym.UUCP> kent at happym.UUCP (Kent Forschmiedt) writes:
>In article <136 at rubmez.UUCP> frei at 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 <pattern> <matchfile> <nomatchfile> [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.



More information about the Comp.unix.wizards mailing list