Gossamer Forum
Home : General : Perl Programming :

playing with txt files

Quote Reply
playing with txt files
I am new to perl and I was wondering if some one could tell me a good place to look for information about reading in text files and formating them.
I wrote a backup program that backs up a database it creates a log file like this each day it looks like this

19 Feb 2001.txt
---------------------------------
(2149 of 2149 pages, 100% complete)
(44 of 44 pages, 100% complete)
Transaction log renamed to: e:\backup\data\01021900.LOG
Database backup completed
----------------------------------------------------

The 4th line should say "Database backup completed" if it does not then I know there is a problem so I was thinking I could write a program to read in that file and say if 4th line doe not = Database backup completed call a sub that sends a email I have written the email part that was easy but now I would like to find some info about reading in txt files in perl or maybe a way to format the txt file and to put it in a HTML table.
Ideas Pointers ?



Quote Reply
Re: playing with txt files In reply to
It may sound like a silly question but do you actually have a book on perl?

The stuff you are talking about is explained in pretty much every introduction to perl book. If you want, take a look at this online perl tutorial
http://www.ebb.org/PickingUpPerl/pickingUpPerl.html

From reading that (or any perl book) you should be able to figure out how to read in and process a text file

Quote Reply
Re: playing with txt files In reply to
Basic formatting would be....

#!/usr/bin/perl

open(FILE,"file.txt") || die "Can't open file:$!";
@file=<FILE>;
close(FILE);

print "Content-type: text/html\n\n";
print "<table>";
foreach $line (@file) {
print "<tr><td>$line</td></tr>";
}
print "</table>";

But again, you can find all this info at perl.com or in a basic perl/cgi tutorial

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: playing with txt files In reply to
open FILE, "<filename.txt";
my @file = <FILE>;
close FILE;

if ($file[3] ne "Database backup completed") {
&send_email ();
}

Jerry Su
widgetz sucks
Quote Reply
Re: playing with txt files In reply to
ok here is my full script but I do not think that it is reading the file in corectly I am sorry but this code might be a bit sloppy I have tried to detail what I am doing here.
The mail part is working fine it the problem could be with the IF statement or readign the file in.

open(FILE,"backup.txt") || die "Can't open file:$!";
my @file = <FILE>;
close FILE;
if ($file[3] ne "hello world") {
&send_email ();
} else {
print "Hello world did not appaer in backup.txt" }

sub send_email {

@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July', 'August','September','October','November','December');

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }

$date = "$days[$wday] $months[$mon] $mday at $hour\:$min\:$sec";

# Define variables ###########################################

$recipients = "Simon\@home.co.uk ";
$fromsender = "Simon\@home.co.uk";
$message = "\"Report of Message Delivery sent on $date\"";
$mailpath = "d:\\dev\\email\\sendemail ";
$server = "172.16.0.2:25";
$subject = "\"Message Received\"";
# Build message ###############################################

$commandline = $mailpath;
$commandline .= "-f $fromsender ";
$commandline .= "-t $recipients ";
$commandline .= "-s $server ";
$commandline .= "-u $subject ";
$commandline .= "-m $message ";

#debug line to show what command is called if problems
print "$commandline \n";

# Send mail using sendemail #####################################

#system($commandline);
exit;
}
Exit;


backup.txt
----------
test1
test2
test3
--------


Quote Reply
Re: playing with txt files In reply to
Hi, you said the problem maybe the IF statement but what actually IS the problem?...are you getting errors or what?

Also instead of......

@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
@months = ('January','February','March','April','May','June','July', 'August','September','October','November','December');

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($hour < 10) { $hour = "0$hour"; }
if ($min < 10) { $min = "0$min"; }
if ($sec < 10) { $sec = "0$sec"; }

$date = "$days[$wday] $months[$mon] $mday at $hour\:$min\:$sec";


........can you not just use $date = scalar localtime;

Also instead of escpaing quotes like.....

$subject = "\"Message Received\"";

.....you could do........

$subject = '"Message Received"'; (I think)
(above is ' followed by " and at the end " followed by ')


Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: playing with txt files In reply to
The problem is that the first part of the script is not running at all the script is ment to read in the file and then if a condition is met eg if the 3rd line in the txt file is XYZ then to run the mail part of the script. but what happens is that the first part does not run only the last part and what ever the file is has on the 3rd line it will send a email you can run the script and test it your self as it is set to just print the command ands does not run it.
Thank you for telling me about the date part but that was how my book on Perl told me to do it the problem is it just seams that there are about 20 ways to do any 1 thing in perl some books say one thing other books say another websites say this and that it can just be hard 4 a newbe starting off to get any where.

Quote Reply
Re: playing with txt files In reply to
In Reply To:
if ($file[3] ne "hello world") {
In Reply To:
backup.txt
----------
test1
test2
test3
--------
'hello world' does not appear in the backup.txt file, therefore the else will never get executed. You're saying that if it is NOT on the 4th line ($file[3] is fouth line, not third) of the file then send the email. Is that what you want?

Also, you are not chomp()ing the lines of the file so therefore your comparison will never match since 'hello world' and 'hello world\n' are two different things.

--mark

Installation support is provided via ICQ at UIN# 53788453. I will only respond on that number.
Quote Reply
Re: playing with txt files In reply to
ok just to say what I want to do is to get the script to read the file below
sorry I was using examples before and might have confused some people here is the exact file
____________________________________________________
(2149 of 2149 pages, 100% complete)
(2 of 2 pages, 100% complete)
Transaction log renamed to: e:\center\01020911.LOG
Database backup completed
______________________________________________________
If the line "Database backup completed" does not appear in this file then
I would like to fire off a email. From The above you can tell the backup worked ok
but if they was ever a problem a would like to have an email sent eg the file might say
_______________________________________________________
Error device not ready
System error
_______________________________________________________

Then I would go and have a look and work out what is wrong.

Simon
The line "Database backup completed" aways appears on the 4th line

Quote Reply
Re: playing with txt files In reply to
How about........

open(FILE,"file.txt") || die "Can't open file.txt:$!";
@file=<FILE>;
close(FILE);

($line1,$line2,$line3,$line4) = split(/\n/,@file);

if ($line4 eq "Database backup complete") {
emailit();
}
else {
emailtheerror();
}



........there is probably an easier way than that though because you don't need to split it but that just gets rid of the \n but you could do as Mark said and chomp it.

Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: playing with txt files In reply to
Does not seam to work aways runs the sub emailtheerror not mater what is in the file.

Quote Reply
Re: playing with txt files In reply to
Make sure that where it says

if ($line4 eq "Database backup complete") {

....is exactly the same as what your script prints.

ie..your script says Database backup completed so

if ($line4 eq "Database backup complete") {

needs to be

if ($line4 eq "Database backup completed") {


Paul Wilson.
new - http://www.wiredon.net
Quote Reply
Re: playing with txt files In reply to
Still no luck please some one test this and see if it works for them
create a file called file.txt then write in it

hello
hello
hello

Save that file then run this script.
this script should check that the 3rd line says hello if it does then it should print ok
but it does not it awasy prints failed there has to be some thing wrong.

open(FILE,"file.txt") || die "Can't open file.txt:$!";
@file=<FILE>;
close(FILE);

($line1,$line2,$line3,$line4) = split(/\n/,@file);

if ($line4 eq "hello") {
print "ok";
}
else {
print "failed";
}

Quote Reply
Re: playing with txt files In reply to
Im not surprised it doesnt say ok because you are asking it to look for hello on line 4 which doesn't exist.

To do it like that you would use:

open(FILE,"file.txt") || die "Can't open file.txt:$!";
@file=<FILE>;
close(FILE);

print "Content-type: text/html\n\n";

foreach $line (@file) {
chop $line;

if ($line eq "hello") {
print "OK";
} else {
print "Not OK";
}

}

This will print OK for each line that says hello so you should end up with...

ok
ok
ok


Paul Wilson.
Installations:
http://www.wiredon.net/gt/
Quote Reply
Re: playing with txt files In reply to
Sorry It was 4 lines in that file.txt and it still did not work
but any way with a little playing about I got your script to work the way I wanted thank you ever so much :-))

It works so I am happy

open(FILE,"p:\\dev\\file.txt") || die "Can't open file.txt:$!";
@file=<FILE>;
close(FILE);

foreach $line (@file) {
chop $line;

if ($line eq "simon") {
print "OK";
exit;
} else {
print "Not OK";
exit;
}
}

Quote Reply
Re: playing with txt files In reply to
No problem.

Paul Wilson.
Installations:
http://www.wiredon.net/gt/
Quote Reply
Re: playing with txt files In reply to
one problem this script does not like looking for files with spaces in it
eg
if ($line eq "Database backup completed")

it returns that it did not find the line in the text file is that because
chop puts the text file in to an array.
eg here is the txt file I am working on
----------------------------------------------------
(2149 of 2149 pages, 100% complete)
(25 of 25 pages, 100% complete)
Transaction log renamed to: e:\scdata\01022700.LOG
Database backup completed
--------------------------------------------------------

And it makes it like this
(2149
of
2149
pages,
100%
complete)
..

should I try and go back to the way the script was and not to use chop ?

Quote Reply
Re: playing with txt files In reply to
It should only be chopping when it finds \n

Paul Wilson.
Installations:
http://www.wiredon.net/gt/
Quote Reply
Re: playing with txt files In reply to
Don't know if this will solve your problem, but...

chop removes the last character, regardless if it is a newline (\n).

chomp only removes the last character if it is a newline (\n).

You should always use chomp over chop if your intentions are to remove \n characters.

Happy Coding,

--Drew
http://www.FindingHim.com
Quote Reply
Re: playing with txt files In reply to
eek sorry my fault.

Yep he needs to change it to chomp $line; then.

Paul Wilson.
Installations:
http://www.wiredon.net/gt/