[net.unix-wizards] Ed

stevesu@bronze.UUCP (Steve Summit) (09/23/83)

Has anyone else been bothered by the following limitation of ed? 
There is no easy way to specify the second (or third or
fourth...) instance of a pattern on a line in an s command. 
(There is probably some horrendous way to do it with \( but it
certainly wouldn't be easy.)  For instance, I have the line

	printf("c[i] = %d = 0%o = '%c'\n", c[i], c[i], c[i]);

and I want to change the last c[i] to c[i++].  In this case I
could distinguish it with the trailing ')', but it's not always
that easy.

(Please don't ask me why I'm using ed.  I think most people would
agree that there are some global type edits that are still best
done with a line based editor.  Of course I use a screen editor
most of the time.)

I've actually considered adding this feature myself, but I
haven't figured out a syntax and I'm not anxious to dive into the
regular-expression code.

Has anyone figured out a decent way to do this with vanilla ed?
Has anyone implemented it?  Do the (apparently) ed-compatible ex
and vi have a pattern-match extension to make it easier?

                                  Steve Summit
                                  tektronix!tekmdp!bronze!stevesu

dan%sri-tsc@sri-unix.UUCP (09/27/83)

The Air Force Data Services Center modified the ed substitute command
to do just what you want (substitute on the n-th instance of a RE
in a line).  In the example you cited, the command would be:

	s5/i/i++/p

to substitute the fifth instance of "i" on the line to "i++".  If you put
a "g" on the end of the line it would substitute the 5th through the
last occurance on the line (which is a noop in this example).

They made lots of other nifty modifications to ed -- the best of which was
to allow you to just say "/string" when searching for "string" and save
the keystrokes of "/p".  Since 80% of what I do in ed is searching, that
turns out to be a big win.

I believe a copy of their code is on some of the earlier USENIX tapes
(three to five years ago).  It's v6 based, but would be worth converting
if you are a big ed fan.

	-Dan Chernikoff (dan@sri-tsc)

gwyn%brl-vld@sri-unix.UUCP (09/27/83)

From:      Doug Gwyn (VLD/VMB) <gwyn@brl-vld>

UNIX System III/V "ed" also lets one type "/pattern" without the
terminating "/p" and other similar shortcuts.  It is far superior
to 7th Edition "ed".  However, I don't believe it can be instructed
to replace the nth occurrence of a pattern, at least not in any
simple way.

ccw@dvamc.UUCP (09/29/83)

References: bronze.772

When trying to change the nth occurance of a string using "ed" I
use "s/string/new_string/i".  "ed" then prints the line, does a
newline and puts some carrot symbols under the section to be
changed.  You the either type "s" or newline.  I'm NOT sure that
the use of "i" in this case is standard.  It may be a local change.
If it is, and there is sufficient interest, I fish out the section
of code that does this and post it.

					Chris Woodbury
					decvax!mcnc!dvamc!ccw

mike@brl-vgr@sri-unix.UUCP (10/02/83)

From:      Mike Muuss <mike@brl-vgr>

Yup;  I added that particular feature to V6 ED a long long time ago
(ca. 1977).  The syntax I chose was:

s delim old-pattern delim new-pattern delim [range] [command]

eg,

s/old/new/2,4p

s/old/new/5

etc.

(Heavily commented) ED source code, upon request, to licensed UNIX sites
(sigh).
			-Mike Muuss

PS:  it is exactly this sort of command that I still drop out of EMACS
and into ED for, every once in a while:

	g/frobnitz/s/this/that/3,7p

dave@rlgvax.UUCP (Dave Maxey) (10/05/83)

ne that matched and either execute or not execute an arbitrary list of
commands - MUCH more powerful than just having verify on substitute), a means
of piping lines to/through filters (like justifiers, spelling checkers), an
"again" command (do the same command again with possibly different addresses),
an ability to go "up a level" to edit another file and then come back down, a
more powerful undo command, a means for reading a list of commands from a
seperate file and returning, etc., etc. ... And we're not even talking about
the things you expect, like line numbers, command prompts, error messages,
turning the meaning of magic characters on and off, and much more ...

	If you're lusting after a version of ed that does all this, why not
just get one? Where, you may ask, is there an ed clone that has all of that?
Here. I happen to have a copy of the latest version of xed, from the author,
tgi (real name Craig Strickland, but nobody seems to know him by that name),
which is version 7.36. Many of you out there have earlier versions. I will
talk with the author and see if there are any problems with distributing to
whoever wants it. I haven't the foggiest who has what rights to it. If you
just want code to modify parts you need, I'll post "ed diffs" in sources.
Sorry for the length of this here, but I wouldn't know where else to post a
reply. Perhaps there should be a net.edclone newsgroup.

			Dave Maxey (alias tbm)
			{seismo,allegra}!rlgvax!dave

m16286@mitre.ARPA (Larry Henry) (02/21/85)

	Why does ED (text editor) not conform to the documentation that
	DEC distributes with it ?? Any ideas/comments I am running ULTRIX

		-Larry.