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.
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?
Thanks in advance Thomas
code example that give a "no match". Fairly easy to read and understand
example 1: ----------- for RFILE in $RLIST; do ed - RFILE <<- EOF H /[matching_text_1]/a This is the appended textline(s) for text1 . /[matching_text_2]/a This is the appended textline(s) for text2 . w q EOF done
Another example that solve the above problem. But this example become very easy unreadable if the appended text is long and/or includes linebreaks.
Example 2: ---------- for RFILE in $RLIST; do ed - $RFILE << EOF g/text1/s//text1\ appended_text1/g g/text2/s//text2\ appended_text2/g w EOF done
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
On Mon, Sep 01, 2008, William L. Maltby wrote:
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.
Instead of ``ed' one can use ``ex'' which should be in any distribution as its part of ``vi'' or ``vim''. I think the command set of ``ed'' is a subset of ``ex'' so scripts should work the same.
I frequently use the gnu shtool program for this sort of thing unless the job is very simple, and I really want to edit the file in place.
Bill
On Mon, 2008-09-01 at 10:26 -0700, Bill Campbell wrote:
On Mon, Sep 01, 2008, William L. Maltby wrote:
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.
<snip>
Instead of ``ed' one can use ``ex'' which should be in any distribution as its part of ``vi'' or ``vim''. I think the command set of ``ed'' is a subset of ``ex'' so scripts should work the same.
Further, it *may* be that the "superset" that is available in ex can overcome of few of the deficiencies and hurdles that his use of ed is presenting. Because of my background, I never read about or learned the full ex stuff - vi(m), sed, (g)awk, bash, ... all did what I needed.
Maybe if the OP reviews the ex stuff he'll have an easier time of it.
Only potential downside I see is maybe a little more overhead and the need for vi(m)/ex on the target system. *Usually* they are there, but maybe someone is a "minimalist" there?
I frequently use the gnu shtool program for this sort of thing unless the job is very simple, and I really want to edit the file in place.
Bill