grep replacement

Geoff Clare gwc at root.co.uk
Tue May 31 23:01:07 AEST 1988


Most of the useful things people have been saying they would like to be
able to do with 'grep' can already be done very simply with 'sed'.
For example:

    Stop after first match:   sed -n '/pattern/{p;q;}'

    Match over two lines:     sed -n 'N;/pat1\npat2/p;D'

It should also be possible to get a small number of context lines by
judicious use of the 'hold space' commands (g, G, h, H, x), but I haven't
tried it.  Anyway, this can be done with a normal line editor (if the data
to be searched aren't coming from a pipe) with 'g/pattern/-,+p'.

I was rather alarmed to see the proposal for 'pattern repeat' in the original
article was '\{pattern\}\1' rather than '\(pattern\)\1', as the latter is
already used for this purpose in the standard editors (ed, ex/vi, sed).
Or was it a typo?

By the way, does anyone know why the ';' command terminator in 'sed' is
not documented?  It works on all the systems I've tried it on, but I
have never found it in any manuals.  It's so much nicer than putting
the commands on separate lines, or using multiple '-e' options.
-- 

Geoff Clare    UniSoft Limited, Saunderson House, Hayne Street, London EC1A 9HH
gwc at root.co.uk   ...!mcvax!ukc!root44!gwc   +44-1-606-7799  FAX: +44-1-726-2750



More information about the Comp.unix.questions mailing list