I'm trying to replace a string of text over multiple files. I think I'm running into problems with forward slashes that happen to be in the string of text I need to replace, but it could be other things as well. Here's the command I've been trying:
perl -e "s/echo '<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http://' . $myserver . '/phpBB2/templates/NoseBleed/FlopTurnRiver.css" type="text/css" -->';/echo '<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http://' . $myserver . '/nosebleed-style.css" type="text/css" -->';/g;" -pi.bak $(find . -type f)
Any suggestions?
echo '<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http://' . $myserver . '/phpBB2/templates/NoseBleed/FlopTurnRiver.css" type="text/css" -->'; the actual string in your files you want to replace?
perl would try to reaplce $myserver with the variable $myserver, which doesn't exist (apart from the slashes), so you'd be better off escaping the $, so \$ (allthough .. \$myserver would be a reference to a scalar ... too drunk to test ... would that lead to further problems? hope not!)
basically, escape every /, . and $ with a \ before it.
you might also be successfull by using \Q...\E to indicate that between those two nothing special is happening ... but I'm usually too lazy to use them and just escape what needs to be
perl -e "s/echo '<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http:\/\/' \. \$myserver \. '\/phpBB2\/templates\/NoseBleed\/FlopTurnRiver\.css" type="text\/css" -->';/echo '<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http:\/\/' \. \$myserver \. '\/nosebleed-style\.css" type="text\/css" -->';/g;" $(find . -type f)
PRODUCED THIS:
Backslash found where operator expected at -e line 1, near "s/echo '<--desktop-setup=horizontal,reverse link rel=stylesheet href=http:// \"
Backslash found where operator expected at -e line 1, near "$myserver \"
(Missing operator before \?)
Bareword found where operator expected at -e line 1, near "/phpBB2/templates"
(Missing operator before templates?)
Backslash found where operator expected at -e line 1, near "// \"
(Missing operator before \?)
Backslash found where operator expected at -e line 1, near "$myserver \"
(Missing operator before \?)
syntax error at -e line 1, near "s/echo '<--desktop-setup=horizontal,reverse link rel=stylesheet href=http:// \"
Can't find string terminator "'" anywhere before EOF at -e line 1.
What is this at the end?
$(find . -type f)
I think the -pi.bak needs to be at the beginning with -e:
perl -pi.bak -e "your code here" input_list
Are you running that on Windows? The double-quotes around the code is Windows specific, otherwise you would use single-quotes.
I'm not sure if you are trying to use concatenation inside the regexp, that will also not work. The dot '.' is a wild card match, not the concatenation operator.
EDIT:
OK, I see it does not work, we were posting at near the same time, your post with all the errors was not there when I first reviewed the thread.
You can try this, but I am not sure if the linux stuff on the end will work:
perl -w -pi.bak -e 's{\Qecho \'<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http://\' . $myserver . \'/phpBB2/templates/NoseBleed/FlopTurnRiver.css" type="text/css" -->\';\E}{echo \'<--desktop-setup=horizontal,reverse link rel="stylesheet" href="http://\' . \$myserver . \'/nosebleed-style.css" type="text/css" -->\';}g' $(find . -type f)
The -w switch will report any warnings when you try and run the code.
If you can explain better what you are trying to do with $(find . -type f) I can probably write it all in perl. I assume that means to find all files in the current directory that are normal files. If that is correct it may be too broad a search unless you are sure you need to edit all the files in the current directory.