meder omuraliev
meder omuraliev

Reputation: 186562

Sed - html replacement

Here is the input I have and the output I want:

Input:

<hr /> 
(newline)
( carriage return)
    (tabs, spaces)<div id="sidebar">

Output:

</div>
<hr />
(newline)
( carriage return)
    (tabs, spaces)<div id="sidebar">

This doesn't seem to match it:

sed -i 's/<hr \/>[[:space:]]*<div id="sidebar">/<\/div><hr \/><div id="sidebar">/g' file.txt

Hrm?

Upvotes: 1

Views: 299

Answers (2)

ghostdog74
ghostdog74

Reputation: 342333

then you don't really need to do substitution. just check for the "<hr >" line, then print "</div>" before it.

awk '/<hr \/>/{    print "</div>" } 1 ' file

Upvotes: 0

chaos
chaos

Reputation: 124287

I don't think you can really do this with sed, because I don't know of any way to convince it to operate on multiple lines at once. It really wants to operate on one line at a time. You can do it reasonably easily with Perl, though:

perl -pi -e 's/<hr \/>\s*<div id="sidebar">/<\/div><hr \/><div id="sidebar">/gs;' -e 'BEGIN { $/ = ""; }' file.txt

Upvotes: 2

Related Questions