grep replacement

Guy Harris guy at gorodish.Sun.COM
Sun May 29 07:58:00 AEST 1988


> grep -n does this, but I'd like to see an option which ONLY prints the line
> numbers where the pattern was found.

I wouldn't - if you're only grepping one file, you can do it without such an
option:

	grep -n <pattern> <file> | sed -n 's/\([0-9]*\):.*/\1/p'

If you're grepping more than one file, you obviously have to decide what you
want to do with the file name and the line number; once you do, just change the
"sed" pattern appropriately (and note that if the list of files is variable,
you either have to stick "/dev/null" in there to make sure the names are
generated even if there's only one file or have the script distinguish between
the one-file and >1-file cases; I seem to remember some indication that the new
BTL research "grep" would have a flag to tell it always to give the file name).



More information about the Comp.unix.questions mailing list