Forum Moderators: coopster & phranque

Message Too Old, No Replies

Search and replace via command line

         

Asia_Expat

12:27 pm on Jul 22, 2014 (gmt 0)

10+ Year Member



I'm trying to replace the HTTP protocol for HTTPS in all the internal links on one of my sites. I'm attempting it with the following but it just throws out errors, I guess the forward slashes. To be clear, I want to change http://www.example.com/ to [example.com...]

This is what I've got so far, this really isn't my areas at all but if I can make it work it'll save me a LOT of time. . .

perl -pi -e 's/"http://www.example.com/"https://www.example.com/g' *.html

phranque

4:59 am on Jul 23, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you might want to use a different delimiter (e.g. a vertical bar instead of a forward slash)

Asia_Expat

9:46 pm on Jul 23, 2014 (gmt 0)

10+ Year Member



Thanks, I'm now using this. . .

perl -pi -e 's|"http://www.example|"https://www.example|g' *.html

It works for the most part and doesn't seem to break anything. The only thing however is it seems to struggle when I use it on .php files when the target string to replace is in an if/else statement i.e. the target is not replaced.

[edited by: phranque at 2:22 am (utc) on Jul 24, 2014]
[edit reason] exemplified domain [/edit]

phranque

2:32 am on Jul 24, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



when the target string to replace is in an if/else statement


i'm not sure what you mean here.
could you provide an exemplified example?

Asia_Expat

3:47 am on Jul 24, 2014 (gmt 0)

10+ Year Member



Sure. The following is the .php include that is pulled into the page template of part of the site. . .


</article><p class="introtext">
Need advice? Got a question? Ask our experts in the <a href="https://www.example.com/forum/country/47-example-forum/">Example Forum</a>
</p><?
$uri=$_SERVER['REQUEST_URI'];
if($uri == "/example/foobar")
echo "<div class='left smalltext tight'>
<h3>Basics</h3>
<h4>Foobar Time</h4>
<p>
GMT +8
</p>
<h4>International dialling code</h4>
<p>
+9999
</p>
<h4>Voltage</h4>
<p>
220 volts, 50 Hertz. The UK standard 3 pin plug design is standard throughout Foobar, a legacy of British colonial rule.
</p>
</div>
<nav>";
else
echo "<nav>
<div class='left smalltext'>
<h3>Top FAQs about Foobar</h3>
<ul>
<li>What is the <a href='https://www.example.com/example/foobar-one'>Foobar Warning System</a> on my TV?</li>
<li>What is the <a href='https://www.example.com/example/foobar-two'>Foobar Mass Transit</a>?</li>
</ul></div>";
?><div class="right textleft smalltext"><h3>Latest Foobar Forum Topics</h3>
<?php
require_once '/home/foobar/public_html/carp/carp.php';
CarpConf('cborder','');
CarpConf('iorder','link');
CarpConf('ilinktitles',0);
CarpConf('bitems','<ul>');
CarpConf('aitems','</ul>');
CarpConf('bi','<li>');
CarpConf('ai','</li>');
CarpConf('maxitems',10);
CarpConf('poweredby','');
CarpConf('carperrors',1);
CarpConf('cacheinterval',5);
CarpConf('encodingout','UTF-8');
CarpCacheShow('http://www.example.com/forum/rss/forums/r4-foobar-forum-latest-posts/');
?></div>
</nav>
<div class="clear"></div></div>


In the above, the http:www.examp is not being replaced with [examp...]
My suggestion it might be something to do with the if/else is just a wild guess.

phranque

5:31 am on Jul 24, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



perl -pi -e 's|"http://www.example|"https://www.example|g' *.html

that's a double quote in front of the http:

Need advice? Got a question? Ask our experts in the <a href="https://www.example.com/forum/country/47-example-forum/">Example Forum</a>

that's a double quote in front of the https:

<li>What is the <a href='https://www.example.com/example/foobar-one'>Foobar Warning System</a> on my TV?</li>
<li>What is the <a href='https://www.example.com/example/foobar-two'>Foobar Mass Transit</a>?</li>
[quote][code]Need advice? Got a question? Ask our experts in the <a href="https://www.example.com/forum/country/47-example-forum/">Example Forum</a>

that's single quotes in front of the https:

CarpCacheShow('http://www.example.com/forum/rss/forums/r4-foobar-forum-latest-posts/');

that's a single quote in front of the http:


so you have every combination except for the pattern specified...

Asia_Expat

5:47 am on Jul 24, 2014 (gmt 0)

10+ Year Member



D'OH!

Asia_Expat

12:42 pm on Jul 24, 2014 (gmt 0)

10+ Year Member



By the way is there a way to make this recursive, avoiding the need to visit every directory?

rish3

12:14 pm on Aug 21, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



By the way is there a way to make this recursive, avoiding the need to visit every directory?


There is, but be careful with it. I would use the -pi.bak syntax to create backup files just in case.

This would run the command, recursively, on every normal file (not on directories), starting with files in the current directory:

find . -type f -exec perl -pi.bak -e 's/was/is/g' {} \;


"normal file" would include "hidden" files, like .htaccess.