Sed / Replace multiline, multiple instances
Hello,
I've written a sed command to match and replace a block of five lines. Until now, the command only replaces one instance of the five lines. What do I need to change to replace all instances? The instances are not entirely the same, but do match the regular expressions. Here is the command file (used with sed -n -f). For better clarity, I do not include the actual regexes. Code:
/regex to match the first line/ { Thanks in advance for your help. jk |
This strikes me as doing things the hard way, but it should work.
Some observations: 1. If the first movement to the hold buffer is "h" and not "H", then you don't need the code to clear the hold buffer. 2. Normally, when you combine multiple lines, you would strip out the newlines before attempting further actions. 3. I don't understand the "p" outside of the {...} construct. Have you tried this in a script instead of having sed call a file? (I don't know if this could be relevant.) The best was to get help on this is to post a sample of the actual file and the specific changes you want to make. |
Thanks a lot for your reply.
I have used sed before for basic find-and-replace tasks, this one being more ambitious. That's why I'm probably missing out on certain skills or best practices in sed scripting. Anyway, here's what I'm trying to do: Starting with a file generated in a markup language (FrameMaker MIF), I'd like to - take some string values (highlighted in red), and - add the same values inside a different markup element to the original file (the whole additions highlighted in blue). The original MIF file has 30000+ lines, so here's an excerpt with the relevant bits on which you can try out the script: Code:
<MIFFile 8.00> Code:
<MIFFile 8.00> Code:
/<XRef\s$/ { I find it convenient using an input file but would be just as happy with a working command line script. I've already changed the first H to lowercase and removed removed the line for cleaing the hold buffer. Thanks again for listening. Maybe you could give me some more hints how to proceed? jk |
Why not do the changes line by line?
eg: sed -e 's/old1/new1/' \ #1st line -e 's/old2/new2/' \ #2nd line -e 's/old3/new3/' . . . etc. |
Code:
|
Thanks very much for your replies.
Pixellany, I could assemble the 'blue' blocks using line-by-line operations, but is it possible at the same time to keep the original lines as consecutive lines? How could I proceed? ghostdog74, As I have no experience with awk so far, I just tried out your sample script without really understanding what it does in particular. The output get a bit mixed up (the original lines do not stay in place, and the substrings extracted from the 1st instance are inserted in the "MText" line for the 2nd instance), but I'll try to give it a shot. jk |
Quote:
Code:
<XRefName `Navigation 4'> |
Quote:
Right at the moment I'm busy with something else, but I'll post again how far I got with sed or awk. jk |
Just wanted to give an update, as I just got it working after staring at the sed command very intensely for a few minutes ...
My multiline script was not the wrong at all, except that the search regex didn't work for all instances :doh:. It works like a charm when I change the following -- Code:
[A-z0-9 ] <= old search regex Code:
/<XRef\s$/ { jk |
All times are GMT -5. The time now is 08:52 PM. |