Solved

Updating links on a webpage?

Posted on 2001-07-17
26
254 Views
Last Modified: 2013-12-25
HI i need a small program that i can run on my server, it should be able to update some links on a .html page.
I dont know programming, so i dont know how difficoult this is..but i would like to get a easy way to update several links on a static webpage...

the program should be able to choose 1 of the files in a folder fx. sport.html  and then change the location of the defined links on this page..

sport.html contains fx 10links that i want to update...I have these links in a txt file, but i think it would be easiest if i could copy the url lines from the txt file into a area of the program, so it will update them one by one..

sport.html should maybe have a template version called sport2.html that is the one that is being updated and saved to the originally file?

please just ask, i dont know if i explained it corectly...


thanks

nick




0
Comment
Question by:nicce
26 Comments
 

Author Comment

by:nicce
ID: 6291703
this is how the sport page could look:
and its only this part that is going to be updated:
http://www.url_1_from_txtfile.html


<html>

<head>
<title>sport pictures</title>
</head>
<body>
<p>&nbsp;</p>
<p align="center">sport pictures</p>
<p align="center">Golf!!</p>
<p align="center"><a href="http://www.other_link_not_updated.html">other link</a></p>
<p>&nbsp;</p>

<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>

</body>
</html>
0
 
LVL 3

Expert Comment

by:interiot
ID: 6291722
What OS?  Would a perl solution be acceptable?
0
 
LVL 3

Accepted Solution

by:
interiot earned 300 total points
ID: 6291755
Here's a perl solution.  I'll be happy to explain how to set it up, if you'll let me know what OS you're running.



#! /usr/bin/perl -w

# What the lines to change start with
my $line_change_start = '<p>&nbsp;New ';

if (@ARGV < 2) {
        print "syntax:  $0 <html file>  <url list file>\n";
        exit(1);
}

my ($html_file, $url_file) = @ARGV;

open HTML, "<$html_file"        or die "$_";
@html_contents = <HTML>;
close HTML;

open URLS, "<$url_file"         or die "$_";
for ($ctr=0; $ctr<@html_contents; $ctr++) {
        $_ = $html_contents[$ctr];
        if (substr($_, 0, length($line_change_start)) eq $line_change_start) {
                my $url = <URLS>;   chomp $url;
                s/href=\s*[^>\s]*/href="$url"/;
                $html_contents[$ctr] = $_;
        }
}
close URLS;

open HTML, ">$html_file"        or die "$_";
print HTML @html_contents;
close HTML;



Run it like this:
     link_updater.pl webpage.html url_list.txt

You may want to change $line_change_start to whatever the lines in the template begin with that you want changed.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:nicce
ID: 6291778
hi my server uses Linux 2.2.18
0
 

Author Comment

by:nicce
ID: 6291786
interiot all solutions are acceptable :) it just needs to work:)

i dont know how your perl script works but it looks simple:) which can be both good bad..hehe

the output html could also look like this so the links needs to be associated with a line in the txt file i think?:
<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.notupdated.html(should not be updated)">mainpage</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>



0
 

Author Comment

by:nicce
ID: 6291794
interiot all solutions are acceptable :) it just needs to work:)

i dont know how your perl script works but it looks simple:) which can be both good bad..hehe

the output html could also look like this so the links needs to be associated with a line in the txt file i think?:
<p>&nbsp;New <a href="http://www.url_1_from_txtfile.html(line 1 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_2_from_txtfile.html(line 2 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_3_from_txtfile.html(line 3 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_4_from_txtfile.html(line 4 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_5_from_txtfile.html(line 5 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.notupdated.html(should not be updated)">mainpage</a></p>
<p>&nbsp;New <a href="http://www.url_6_from_txtfile.html(line 6 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_7_from_txtfile.html(line 7 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_8_from_txtfile.html(line 8 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_9_from_txtfile.html(line 9 from txt file)">golf pictures</a></p>
<p>&nbsp;New <a href="http://www.url_10_from_txtfile.html(line 10 from txt file)">golf pictures</a></p>



0
 

Author Comment

by:nicce
ID: 6291824
the links in the template file should maybe look like this:
<a href="#%URL1%#">golf pictures</a></p>
<a href="#%URL2%#">golf pictures</a></p>
<a href="#%URL3%#">golf pictures</a></p>

<a href="http://www.mainpage.com">Back to mainpage!</a></p>

<a href="#%URL4%#">golf pictures</a></p>
<a href="#%URL6%#">golf pictures</a></p>
<a href="#%URL7%#">golf pictures</a></p>

<a href="http://www.yahoo.com">Visit Yahoo.com!</a></p>

ect....
0
 
LVL 3

Expert Comment

by:interiot
ID: 6291878
Ahh, that's a good idea.  I changed it to use your template idea, and I commented it a bit.



#! /usr/bin/perl -w

if (@ARGV < 2) {
        print "syntax:  $0 <html file>  <url list file>\n";
        exit(1);
}

# Get the command line arguments
my ($html_file, $url_file) = @ARGV;

# Read in the entire html file
open HTML, "<$html_file"        or die "$_";
@html_contents = <HTML>;
close HTML;

# Read in the entire URL file
open URLS, "<$url_file"         or die "$_";
@urls = map {chomp;$_} <URLS>;
close URLS;

# Go through the html file, line by line.
for ($ctr=0; $ctr<@html_contents; $ctr++) {
        # Replace #%URL3%# with the contents of $urls[3]
        $html_contents[$ctr] =~ s/#\%URL(\d)\%#/$urls[$1]/ge;
}

# Write the contents of the html file back out
open HTML, ">$html_file"        or die "$_";
print HTML @html_contents;
close HTML;
0
 
LVL 3

Expert Comment

by:interiot
ID: 6291891
To get the script to work, cut and paste the code to, say, link_update.pl.  Then run "chmod +x link_update.pl".  Then you should be able to simply type the
     link_updater.pl template.html url_list.txt

When it's done, template.html will be changed.  Or perhaps you want there to be an input template file and an outputted html file?
0
 

Author Comment

by:nicce
ID: 6291894
Hi sorry i dont know what chmod +x link_update.pl means
im not doing the tech on my server.:)
maybe you can explain it a little moe how i uses this...
i dont have much more experience than usinf ftp and setting file permissions..!
0
 

Author Comment

by:nicce
ID: 6291899
it would be nice with a output html file, so i can check if its ok and then i can rename it myself afterwards:)
0
 

Author Comment

by:nicce
ID: 6291907
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
 

Author Comment

by:nicce
ID: 6291920
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
 

Author Comment

by:nicce
ID: 6291925
and another thing there will be more than 1 template..1 for each topic...so it would be cool with a interface where i can choose the template, and maybe a output file name?
maybe this to much to ask, but i dont know how hard this is, but sofar thank you very much:)
0
 
LVL 3

Expert Comment

by:interiot
ID: 6294492
Okay.  

link_update.pl:
---------------------------------------------------------------
#! /usr/bin/perl -w

if (@ARGV < 3) {
        print "syntax:  $0 <template filename>  <url list filename> <output filename>\n";
        exit(1);
}

# Get the command line arguments
my ($template_file, $url_file, $output_file) = @ARGV;

die "template file '$template_file' doesn't exist" if (! -e $template_file);
die "url list file '$url_file' doesn't exist"      if (! -e $url_file);

# Read in the URL list
open URLS, "<$url_file"         or die "$_";
@urls = map {chomp;$_} <URLS>;
close URLS;

# Read in the template, write to the output file
open TEMPLATE, "<$template_file"        or die "$_";
open OUTPUT, ">$output_file"            or die "$_";
while (<TEMPLATE>) {
        # Replace #%URL3%# with the contents of $urls[3]
        s/#\%URL(\d)\%#/$urls[$1]/ge;

        print OUTPUT $_;
}
close OUTPUT;
close TEMPLATE;



menu:
---------------------------------------------------------------
#! /bin/sh

dialog \
        --menu "Select which page to update the links for\\n" \
        23 80 4 \
        index   'Update homepage'               \
        page1   'Update the first subpage'      \
        page2   'Update the second subpage'     \
        page3   'Update the third subpage'      \
        2>/tmp/link_update_menu.out     || exit

filename=`cat /tmp/link_update_menu.out`
test -z "$filename" && exit

link_update.pl templates/$filename.html urls/$filename.urllist out/$filename.html

echo "$filename.html updated".

---------------------------------------------------------------


You'll want to modify the menu script quite a bit.  The 4  in "23 80 4" is the number of menu options that follow.  Then there's a list of files and descriptions of the files to pick from.

As it's set up, if you select 'index', it will use the template from templates/index.html, the URL list from urls/index.urllist, and output the results to out/index.html.
0
 
LVL 3

Expert Comment

by:interiot
ID: 6294503
Oops.  The last two lines of the menu script should be:

link_update.pl templates/$filename.html urls/$filename.urllist out/$filename.html \
        && echo "$filename.html updated".
0
 

Author Comment

by:nicce
ID: 6294980
great let me try to set it up:)..
do you have icq? it would be easier if i need some support:)?
0
 

Author Comment

by:nicce
ID: 6295012
should i just upload it  and execute link_update.pl thru my browsor or?

it just shows the code in the window..??
0
 

Author Comment

by:nicce
ID: 6295103
should i just upload it  and execute link_update.pl thru my browsor or?

it just shows the code in the window..??
0
 

Author Comment

by:nicce
ID: 6295200
sorry for the double posts, i dont know why they appear..weird!!
0
 
LVL 3

Expert Comment

by:interiot
ID: 6295365
No, I don't have ICQ set up at work.

I errored and wrote the program for the command line.  If you run it without modification, you'll have to just run 'menu' from the command line.
0
 

Author Comment

by:nicce
ID: 6295759
sorry, what do you mean by command line? in dos or what?
0
 
LVL 3

Expert Comment

by:interiot
ID: 6295767
Unix has a command line, similar to dos, but different.
0
 

Author Comment

by:nicce
ID: 6295972
oh ok..i dont have access to anything on the server:(
what im interested in is a bowsor based update utillity:)
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6699416
Please update/finalize this question .... no activity since 7/18/01.  Also, you may benefit by checking the All Topics area to see if another topic area may fit.

Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6903876
Comment from expert accepted as answer.

Thank you
Computer101
E-E Moderator
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
view results of google SQL query 9 67
Register AutoHotkey 12 85
User profile Size Report 3 84
Questions about this React unit test 4 67
In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question