Article View: demon.ip.developers
Article #171Re: grep 'n edit, I wish!
From: Chris Croughton
Date: Wed, 11 Nov 1998 00:00
Date: Wed, 11 Nov 1998 00:00
81 lines
3628 bytes
3628 bytes
In article <910784101snz@wash.demon.co.uk>
john$@wash.demon.co.uk "John Washington" wrote:
> grep foobar *.c
> [pause to eyeball result]
> edit thisfile.c thatfile.c tother.c yafile.c etc.c
>
>And that really bugs me. The damn computer knows which files
>I want already. I could even have used the -l option in grep
>to get a little list of them.
You mean:
edit `grep -l foobar *.g`
?
>For several years the synapses down the far end of my tail have
>been reckoning it should be a SMoP to somehow generate that
>command line for me. I could just type
>
> e foobar
Well, you could alias it...
>I can't stand it any longer. The message has at last got up to
>my brain that if I don't know how to do it, one of you can, Shirley?
>
>Oh, and yes OF COURSE it's easy-peasy in Unix, but being a
>dinosaur I'm running msdos.
Not a problem, there's a port of GNU C called DJGPP which emulates Unix
type command lines. It would be easy to write a program which took the
appropriate command line (including the backquotes to run grep or
whatever) and called your editor with the result as the arguments. The
only thing you have to be careful about is that most MSDOS programs can't
handle and argument list longer than 127 characters - you could of course
make it call the editor once for each file instead.
It may also be possible to do something similar using a 4DOS batch file
(if you are still using COMMAND.COM, seriously you should use 4DOS
instead). In fact I'm pretty sure it can be done fairly easily,
especially in recent (v6) versions.
In fact:
@echo off
setlocal
grep -l "%1" %2 > \tmp\1
set x=
do i = 0 to %@lines[\tmp\1]
set x=%x %@line[\tmp\1,%i]
enddo
edit %x
Works for me (it does assume that you don't mind overwriting file \tmp\1,
and it calls 'edit' with the list of files which probably won't work for
you, but those can be easily hacked). You call it as
editgrep foobar *.[ch]
>Can anyone help? Please?? It would make my life so much more
>pleasant when browsing code such as Snews and Ka9q. Phew! At
>last something to get on-topic with IP. :-)
<g> Thanks for the idea - now I'll rewrite it a lot more efficiently (and
a lot less understandable) as a bash script (did I mention that there's a
DJGPP port of bash - the GNU version of the Bourne shell - for DOS as
well?). And then the zsh version which will be all on one line <g>...
>P.S. What do others feel? Is this kind of digression unwelcome,
>or should we sometimes use this as a forum to exchange useful
>development tools, tips and tricks? Or are there better places
>in Usenet for this?
There probably are, but I certainly don't see them (far too high traffic,
for instance on the DJGPP newgroup). As far as I'm concerned, the
occasional request of this nature isn't exactly overloading the newsgroup,
in fact it's nice to have some reassurance that it's still there...
Chris C
Technical Details
Message-ID:
<Snews.981111.195942.chris.11264@keris.demon.co.uk>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<910784101snz@wash.demon.co.uk>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10bb05,f355ae5d069fba47 X-Google-Attributes: gid10bb05,public From: chris@keris.demon.co.uk (Chris Croughton) Subject: Re: grep 'n edit, I wish! Date: 1998/11/11 Message-ID: <Snews.981111.195942.chris.11264@keris.demon.co.uk> X-Deja-AN: 411151273 X-Mail2News-Path: news.demon.net!keris.demon.co.uk References: <910784101snz@wash.demon.co.uk> X-Trace: mail2news.demon.co.uk 910892903 mail2news:27946 mail2news mail2news.demon.co.uk Organization: Keristor Systems Newsgroups: demon.ip.developers X-Complaints-To: abuse@demon.net