[CentOS] Help using ed [OT]

William L. Maltby

CentOS4Bill at triad.rr.com
Mon Sep 1 10:56:51 UTC 2008


On Sun, 2008-08-31 at 19:45 +0200, Thomas Johansson wrote:
> Hi
> 
> Perhaps omeone might answer this tricky problem. I can do this other 
> ways, but i really want to understand how to solve it using ed. I have 
> one solution using g/re/s/re//txt/ , but I want to understand how or if 
> i can solve it using the ed (.)a command.

OK. But keep in mind that many folks (and distros?) believe ed is
hopelessly outdated and some even advocate its removal from
distributions. Thankfully some other packages(s) depend on it.

You would be better off using other techniques, sed, (g)awk, some minor
programming language, perl, ...

Regardless, you can't do it precisely as you desire do to the nature or
ed. It's a pure context-sensitive editor.

Fortunately us outdated relics hang together and I can offer a solution.
Whether it is acceptable to you is another matter. More discussion
below.

> 
> A script i have parse several files and append text after a specific 
> text is matched. If one file do not have this text, i get a no match and 
> the script terminates. How can I avoid this behavior keeping the 
> original coding style for ed and append text. I have tried tried but 
> still cannot get it correct using (.)a. Is it even possible to do it 
> that way using the (.)a command?

You have a basic problem with trying to do it without commands using a
range, such as those in your second example.

>From "man ed":

DIAGNOSTICS
       When an error occurs, if ed’s input is from a regular file or
       here document, then it exits, otherwise it prints a ‘?’ and
       returns to  command mode. An  explanation  of  the last error can
       be printed with the ‘h’ (help) command.

>From 1) memory, 2) recent review of man page, there is no way around
this.

Further, your specification of the file leaves a lot to the imagination.
If there are more than 26 instances of addresses you need to add some
more to my sample script to remove existing marks, after they are done
being used, and mark again. Repeat, rinse until done.

If there is more than one occurrence of any pattern, my sample is
inadequate, due to the use of marks to get around ed's inability to
handle a not-found silently and gracefully. This example will process
only the last occurrence.

Although ed is very fast, if the target files are large this may be slow
due to the repeated traversal of the buffers. With a limit of only 26
items (in my example) this may not be an issue.

Once again, you really need to be using some stream processor, as
suggested above.

> 
> Thanks in advance
> Thomas
> 
> <snip code example in consideration of those with no interest>

I have attached my example and test cases so those with no interest
don't have to read.

HTH
-- 
Bill
-------------- next part --------------
aa
bb
cc
-------------- next part --------------
aa
bb
cc
bb
-------------- next part --------------
bb
cc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test
Type: application/x-shellscript
Size: 322 bytes
Desc: not available
URL: <http://lists.centos.org/pipermail/centos/attachments/20080901/986965b3/attachment.bin>


More information about the CentOS mailing list