Re: A little sed
On Fri, Jan 26, 2001 at 11:19:53AM +0100, Hans wrote:
> - I need to clean up a bunch of html files from <SCRIPT> </SCRIPT> tags. I
> tried sed -e s/\<SCRIPT.*SCRIPT\>// file.html > file.html2, but it only
> deletes the first line, not the whole script. The /m modifier doesn't seem
> to work. How do I go about it.
Can't say much on this without seeing the data you're trying to mung.
> - Is it possible to overwrite the original file, not redirect to an
> alterate file?
>
> - How do I process a bunch of files at once? sed -e s/foo/bar/ *.html >
> *html2 doesn't seem to do it. Or need it be something like for $i in *
> do....? I can't seem to get this to work either.
I can help you with these two, though. Below, you'll find doall.pl, a perl
script I hacked up for a similar situation (sedding a bunch of C source
files, in my case). Usage is simply
doall.pl "sed -e '<your regex here>'" *.html
--- begin script ---
#!/usr/bin/perl -w
use strict;
my $cmd = shift;
while (my $filename = shift) {
# Perform operation to new file. Exit on error.
# TODO: On error, report command that caused problem and the resulting
# error message before dying.
if (`$cmd $filename 2>&1 >$filename.new`) { die };
# If the output is different than the input, replace the old version with
# the new one. If nothing was changed, discared the new version and leave
# the old one untouched.
if (`diff $filename.new $filename`) {
rename "$filename.new", $filename;
} else {
unlink "$filename.new";
}
}
--- end script ---
--
SGI products are used to create the 'Bugs' that entertain us in theatres
and at home. - SGI job posting
Geek Code 3.1: GCS d? s+: a- C++ UL++$ P++>+++ L+++>++++ E- W--(++) N+ o+
!K w---$ O M- V? PS+ PE Y+ PGP t 5++ X+ R++ tv b+ DI++++ D G e* h+ r y+
Reply to: