I want to insert into a html source file a marked-up (into html) other text file of unknown length, but always at least two lines. I was going to use m4, but "include" reads the whole file AFAIK. So, on to sed...
Once I have found the pattern that indicates the start of the insertion point, the first line will be appended to <div class=...>
tags, and the second similarly (but different class), and then loop until EOF, then the rest of the source file is output.
Finding the insertion point is ok, as is printing the remainder of the source file. I am having a problem with sed looping to read in the text file until it is done.
Example input
title1
author1
title2
author2
...
titleN
authorN
Desired output
<!-- above here is source file, below is sed'ed output -->
<div class="title">
title1
</div>
<div class="author">
author1
</div>
<div class="title">
title2
</div>
<div class="author">
author2
</div>
...
<div class="title">
titleN
</div>
<div class="author">
authorN
</div>
<!-- below is rest of source file -->
I am not too concerned with line breaks, all on one line is fine, the example is just to make it clear what is going on. `
I can get it to work fine with a \ <div ....
and R filename
and so on with the simple case of two or four lines of input. As soon as I try to use a loop to handle the case of a variable number of lines of input, I fail.
I tried using a dummy substitution s|^\(.+\)|\1|
so I can test it with T
and exit if the pattern match was empty, but it doesn't work. My other attempt resulted in sed going into an infinite loop.
How can you test whether R
succeeded or failed? Is there a design pattern I am missing here?
(I'm using GNU sed, so R
and T
are ok.)
Thanks.