in reply to need help on SED
You call sed like this:
This is generally considered a bad and very unsafe way, not to mention at times unpredictable. What if $search, $replace, or any other variable has the value ";; rm -rf / ;;" ? What will the command line be equivalent to? You should call external programs using a method that does not use the shell, or automatically espaces arguments. May I suggest:system("sed -e 's/$search/$replace/g' $File1 > $Replace_html");
This open call executes sed with the arguments you want, and sends its output back do the filehandle $fh. You don't have to worry about the shell messing up the characters in the arguments, because this mechanism bypasses the shell, making it very safe.open (my $fh, "-|", 'sed', '-e', "s/$search/$replace/g", $File1); my $new_file = do { local $/; <$fh> }; # grab sed's output # now you can write the contents of $new_file to a file yourself.
Another alternative would be to use IPC::Open2 or IPC::Open3, which will let you execute a command in the same safe way as above, and send its output to the file all in one statement. But I'll leave that as an assignment for you -- it's a very similar mechanism, however.
Also, the next time you submit a question (or an answer?) that contains code, use <code> tags, and check the previews of your post to make sure they look readable! Hey, is there an echo in here? ;)
blokhead
|
|---|