Gossamer Forum
Home : General : Perl Programming :

Copying Information from One File to Another with Different Format

Quote Reply
Copying Information from One File to Another with Different Format
I am attempting to copy email addresses from one .txt file into a .htpasswd file that has passwords associated with email addresses. What I have running is Subscribe Me! by cgielite.com for a mailing list. The .htpasswd file is used for subscribers to access a password protected directory via .htaccess. This .htpasswd file has the same password for each email address. I know... not the most secure method, but I am trying to at least provide some benefit of subscribing to my mailing list. The files located in the password protected directory are archives of my company Newsletter.

Anyway...does anyone know how to create a cgi file that will take the email addresses from the .txt file and copy them to the .htpasswd file and at the same time add a common password to each email address?

In the text file, the format is:

Code:
myname@server.com
joesmo@server.com

The format in the .htpasswd is:

Code:
myname@server.com:YHEDNFG.YETS2ys
joesmo@server.com:YHEDNFG.YETS2ys

Any assistance or pointers is greatly appreciated.

TIA.

Best regards,



------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 06, 1999).]
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Try this:
Code:
open (TXT, "file.txt");
@lines = <TXT>;
close TXT;
open (HT, ".htpasswd");
foreach $line (@lines)
{
print HT "$line:YHEDNFG.YETS2ys\n";
}
close HT;


------------------
The Flying Cranes, Inc:
http://www.theflyingcranes.com
Perl scripts & Web promotion:
http://find.virtualave.net
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Thanks, Pasha...I did look at the "delete file" script, but it wasn't exactly what I was looking for. I guess your codes are good starting point. I will try it to see if it works.

Thanks.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
And you might want to change this:
Code:
open (HT, ".htpasswd");

to this:

Code:
open (HT, ">>.htpasswd");

the server will be forced to create this file Smile

------------------
The Flying Cranes, Inc:
http://www.theflyingcranes.com
Perl scripts & Web promotion:
http://find.virtualave.net
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Try this:
Code:
unlink (".htpasswd"); # Delete the file.
open (TXT, "file.txt");
@lines = <TXT>;
close TXT;
open (HT, ">>.htpasswd"); # Create and write to the file.
foreach $line (@lines)
{
print HT "$line:YHEDNFG.YETS2ys\n";
}
close HT;
chmod (0777, ".htpasswd"); # Chmod the file to 777.

I have no idea what CRON is. Have you got a link to some online CRON FAQ?

------------------
The Flying Cranes, Inc:
http://www.theflyingcranes.com
Perl scripts & Web promotion:
http://find.virtualave.net
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
 
Quote:
open (HT, ">>.htpasswd"); # Create and write to the file.

Will open a file for appending. If it does not exist, it is created.

Quote:
open (HT, ">.htpasswd"); # Create and write to the file.

Will create a file if it does not exist and will overwrite a file that does exist (saving you the code to delete it first).

I hope this helps.
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Pasha,

Cron is a method of automatically running cgi files, such as the nph-build.cgi file in LINKS. Cron is a server operation that updates files, makes backups, etc. If I can get this cgi file to work, then I should have no problem running it as a cronjob.

For more information on Cron, check out the Cron FAQ in this Site. Go to the Resource Center, then search for Cron.

Thanks for your further input.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Thanks, Pasha...The .htpasswd file already exists. Yet, it would be nice to delete the .htpasswd file first, then copy the data from the .txt file, and then finally change permissions of the .htpasswd file.

In terms of file permissions...I currently have the .htpasswd file set to 777.

Do you know of way that the script can change permissions of the file after the .htpasswd file has been created?

I am planning on running this file as a cron job. Do you see any potential problems in doing this?

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 07, 1999).]
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Well, I have experimented with the combined codes offered by Pasha and Bobsie. The file does copy over to the .htpasswd, but not in the correct format.

The format should be:

emailaddress:password

What I get is:

emailaddress
:password

(On two different lines)

Here are the codes I am using:

Code:
$txtfile = "/mnt/web/guide/anthrotech/private/address.txt";
$other = "/mnt/web/guide/anthrotech/resources/atdigest/archives/.htpasswd";
$password = "YLE9V7.WYTRErs";

# Opens Text File
open (TXT, "$txtfile") or &error
("unable to open $txtfile/$_. reason: $!");
@lines = <TXT>;
close TXT;

# Create and write to the file.

open (HT, ">$other") or &error
("unable to open $other/$_. reason: $!");
foreach $line (@lines) {
print HT "$line:$password\n";
}
close HT;

&print_log;


sub print_log {
#----------------------------------------------------------
# Prints a log file to keep track of copied files.
#

print "Content-type: text/plain\n\n";
print "$txtfile copied to $other.\n";
print "Check the $other to verify data transfer.";

}


sub error {
# --------------------------------------------------------
print "Error: $_[0]\n";
exit;
}

Anyone have suggestions about how to clean up the format in the .htpasswd file??

Thanks.

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 07, 1999).]

[This message has been edited by Eliot (edited August 07, 1999).]

[This message has been edited by Eliot (edited August 07, 1999).]
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Eliot,

I bet that each of the lines you read in have a \n on the end.

Try this:

Code:
# Create and write to the file.
open (HT, ">$other") or &error ("unable to open $other/$_. reason: $!");
foreach $line (@lines) {
chomp ($line);
print HT "$line:$password\n";
}
close HT;

I hope this helps.
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Thank you, Bobsie. Smile

It worked like a charm. I was wondering if you have the time, if you would be willing share possible impacts on a server with setting up a cron job that runs every hour.

Also, if you are willing to share with us what "chomp" stands for and what it does. Yes, I probably could read a perl tutorial and find the answer. But if you are willing and able to put it in dummy terms (K.I.S.S), I would greatly appreciate it.

I am assuming that chomp takes out special characters and re-prints them in a different format. But this is a stab in the dark.

TIA.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us

[This message has been edited by Eliot (edited August 07, 1999).]
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
chomp() is usually used to remove a newline (/n) at the end of a string. See:

www.perl.com/pub/doc/manual/html/pod/perlfunc/chomp.html

adam
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Thanks, dahamsta.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Here is the "official" description of chomp from perldoc:

Code:
chomp VARIABLE
chomp LIST
chomp This is a slightly safer version of the chop entry elsewhere
in this document . It removes any line ending that
corresponds to the current value of `$/' (also known as
$INPUT_RECORD_SEPARATOR in the `English' module). It
returns the total number of characters removed from all
its arguments. It's often used to remove the newline
from the end of an input record when you're worried that
the final record may be missing its newline. When in
paragraph mode (`$/ = ""'), it removes all trailing
newlines from the string. If VARIABLE is omitted, it
chomps `$_'. Example:

while (<> ) {
chomp; # avoid \n on last field
@array = split(/:/);
# ...
}

You can actually chomp anything that's an lvalue,
including an assignment:

chomp($cwd = `pwd`);
chomp($answer = <STDIN> );

If you chomp a list, each element is chomped, and the
total number of characters removed is returned.

In other words, it removes the last character from a line, usually a newline character. My experience is, it will remove any last character from a line.

Quote:
I was wondering if you have the time, if you would be willing share possible impacts on a server with setting up a cron job that runs every hour.

It depends on the application that would run every hour. For example, it might not be too bad to run nph-build.cgi once an hour (depending on how many other people are on the same server), but it would really not be a good idea to run nph-verify.cgi once an hour. If you have an idea of the resources it takes to run a particular app, you can better judge whether or not in will adversely impact on a server to run it frequently. You would need to know the memory capacity of the server and how much memory your app uses as well.

[This message has been edited by Bobsie (edited August 08, 1999).]

[This message has been edited by Bobsie (edited August 08, 1999).]
Quote Reply
Re: Copying Information from One File to Another with Different Format In reply to
Thanks, Bobsie.

I believe that my server has a cap on the number times you can run a cron job per day.
I tried setting a cron job for this particular cgi file to run every hour. I got an error message when I tried to install this cron job. So, I changed it to every two hours, and it worked.

Thanks again. I thought an explanation would not only serve my learning experience, but others, too.

Regards,

------------------
Eliot Lee
Founder and Editor
Anthro TECH, L.L.C
http://www.anthrotech.com/
info@anthrotech.com
==========================
Coconino Community College
http://www.coco.cc.az.us/
Web Technology
Coordinator
elee@coco.cc.az.us