sed 's/^ \{2,\}//' -i filename.html
But that exact same code doesn't run when it's in a Perl script. The file doesn't get modified. In the script, I enclose the code in backticks and add a semicolon at the end, that's the only difference. No error message, the script itself runs, but my file doesn't get changed.
I did some troubleshooting, removing the -i flag and assigning the output of sed to an array, and I found that the backticked sed commands worked fine on very simple pattern matches, but they failed when I either used the ^ operator to signal the start of a line, or the \{2,\} notation to specify 2 or more matches.
What am I doing wrong?
I realize that I could manually open each file, read it into a variable, process it, and save it back to disk, but that seems cumbersome when I should be able to do it with one line with sed. The manual method also seems slower and a lot more memory intensive. I'll actually be processing thousands of files this way. But if you know of a more efficient way to do this from within Perl, I'm all ears.
[perl.com...]
{
local $^I = '.bak'; # emulates -i.bak
local @ARGV = glob("*.html"); # get list of .html files
while (<>) {
s/^ {2,}//;
print;
} continue {close ARGV if eof}
}
I guess my question should be: Given filename "$filename", what's the quickest/easiest way to run a search & replace on it from a Perl script?"