I am writing a Perl script to change all URLs within several files in one directory from camel case to lowercase.
e.g. from
<a href="FooBar.html">
to
<a href="foobar.html">
This test substitution correctly renames all URLs to foobartest.html as expected:
s/^<a href=\"(.*?)\"/<a href=\"foobartest.html\"/g
But I am having difficulty in referencing the actual filename via the variable $1. An example of what I am trying:
s/^<a href=\"(.*?)\"/<a href=\"\L$1\"/g
This substitution merely changes the link to . What am I missing?
You can probably tell I’m fairly new to Perl, and so any guidance would be much appreciated.
Full script for reading files within a directory and substituting (writing it as a one-liner does not work either, despite other substitutions working via one line):
#!/usr/bin/perl
use strict;
use warnings;
chdir("/var/www/html/twiki_html") or die "$!";
opendir (DIR, ".") or die "$!";
my @files = grep {/.*?\.html/} readdir DIR;
close DIR;
{
local @ARGV = @files;
while(<>){
s/^<a href=\""(.*?)\"/<a href=\"\L$1\"/g;
}
}
A simple one-liner:
It will not change anything, just display the changes. So, when you are satisfied it works, you can add
-ito the options to make changes to the files. Be aware that changes are irreversible. Use-i.bakto keep backups. E.g.: