tags:

views:

116

answers:

2

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?

+2  A: 

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
chaos
Using N, you can make sed work on multiple lines, but perl is definitely the right way to go here.
William Pursell
interesting. what would be a strategy for using this operation on multiple files?find . -maxdepth 1 -type f -exec ( your command ) {} \; ?
meder
awesome that worked.
meder
A: 

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
ghostdog74