perl -0777 -i.withdiv -pe 's{<div[^>]+?id="user-info"[^>]*>.*?</div>}{}gsmi;' test.html
-0777
means split on nothing, so slurp in whole file (instead of line by line, the default for -p
-i.withdiv
means alter files in place, leaving original with extension .withdiv (default for -p is to just print).
-p
means pass line by line (except we are slurping) to passed code (see -e)
-e
expects code to run.
man perlrun
or perldoc perlrun
for more info.
Here's another solution, which will be slightly more familiar to people that know jquery, as the syntax is similar. This uses Mojolicious' ojo
module to load up the html content into a Mojo::DOM object, transform it, and then print that transformed version:
perl -Mojo -MFile::Slurp -E 'for (@ARGV) { say x(scalar(read_file $_))->at("#user-info")->replace("")->root; }' test.html test2.html test*.html
To replace content directly:
perl -Mojo -MFile::Slurp -E 'for (@ARGV) { write_file( $_, x(scalar(read_file $_))->at("#user-info")->replace("")->root ); }' test.html
Note, this won't JUST remove the div, it will also re-write the content based on Mojo's Mojo::DOM module, so tag attributes may not be in the same order. Specifically, I saw <div id="user-info2" class="logged-in">
rewritten as <div class="logged-in" id="user-info2">
.
Mojolicious requires at least perl 5.10, but after that there's no non-core requirements.