Welcome to WebmasterWorld Guest from 34.238.189.171

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

Search and replace via command line

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

Preferred Member

10+ Year Member

joined:Dec 7, 2005
posts:636
votes: 0


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
4:59 am on July 23, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11771
votes: 224


you might want to use a different delimiter (e.g. a vertical bar instead of a forward slash)
9:46 pm on July 23, 2014 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 7, 2005
posts:636
votes: 0


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]

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

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11771
votes: 224


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?
3:47 am on July 24, 2014 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 7, 2005
posts:636
votes: 0


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.
5:31 am on July 24, 2014 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11771
votes: 224


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...
5:47 am on July 24, 2014 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 7, 2005
posts:636
votes: 0


D'OH!
12:42 pm on July 24, 2014 (gmt 0)

Preferred Member

10+ Year Member

joined:Dec 7, 2005
posts:636
votes: 0


By the way is there a way to make this recursive, avoiding the need to visit every directory?
12:14 pm on Aug 21, 2014 (gmt 0)

Preferred Member

5+ Year Member Top Contributors Of The Month

joined:May 24, 2012
posts:648
votes: 2


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.