Gossamer Forum
Home : General : Perl Programming :

Help with this script - open and print data from multiple files

Quote Reply
Help with this script - open and print data from multiple files
I found this script and I need it to do a couple of things differently. It opens each file in the directory and prints "only the first line of data" from each file into a file named report.txt - I am using it to open log files on my server stored by date, such as 2008-05-10.log

1) I want it to print all lines from each file (each file has several lines of data in it), in to report.txt (and not just the first line from each file).
2) Also, it prints in report.txt, the name of each file at the beginning of each line like so, filename:followed by the data and I don't want it to print the file names (2008-05-10.log:) at the start of each line.

Additionally, If possible (but this is optional as I can do this by importing in excel):
Each line in the log file has several fields like so - keyword&&URL&&...etc., and I would like it to eliminate duplicate lines of data "if the keyword (the first-field in each line) is the same.

Thanks for your help in advance.

#!/usr/bin/perl
# here we go ho ho
&read_file("/home/bizspi/www/cgi-bin/logs","report.txt");
sub read_file{
my ($directory, $yourfilename) = @_;
opendir (DIR, $directory) or die ("Can't open dir: '$directory'.\nReason: $!");
my @ls = readdir(DIR);
closedir (DIR);
my $fullfile;
my (@total_list,$total,$tmp);
foreach $file (@ls){
$fullfile = "$directory/$file";
if(-f $fullfile){
open (DATA,"$fullfile") or die("Can't open file: '$directory/$fullfile'.\nReason: $!");
$lines = <DATA>;
#$total += scalar($line);
$file .= ":$lines";
push (@total_list,$file);
close DATA;
}
}
#write to file
open (DATA,">$directory/$yourfilename") or die("Can't open write to file: '$directory/$yourfilename'.\nReason: $!");
foreach $line (@total_list) {
print DATA "$line\n";
}
#print DATA "Total: $total\n";
close DATA;
}

Quote Reply
Re: [socrates] Help with this script - open and print data from multiple files In reply to
Hi,

I've cleaned up the code (formatted, etc) - so its easier to read.

It looks like it already gets ALL the contents of the files it reads, and puts them into report.txt?

Code:
#!/usr/bin/perl

&read_file("/home/bizspi/www/cgi-bin/logs","report.txt");

sub read_file{

my ($directory, $yourfilename) = @_;
opendir (DIR, $directory) or die ("Can't open dir: '$directory'.\nReason: $!");
my @ls = readdir(DIR);
closedir (DIR);

my $fullfile;
my (@total_list,$total,$tmp);

foreach $file (@ls){
$fullfile = "$directory/$file";
if (-f $fullfile) {
open (DATA,"$fullfile") or die("Can't open file: '$directory/$fullfile'.\nReason: $!");
$lines = <DATA>;
$file .= ":$lines";
push (@total_list,$file);
close DATA;
}
}

# write to file
open (DATA,">$directory/$yourfilename") or die("Can't open write to file: '$directory/$yourfilename'.\nReason: $!");
foreach $line (@total_list) {
print DATA qq|$line\n|;
}
#print DATA "Total: $total\n";
close DATA;
}

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Help with this script - open and print data from multiple files In reply to
Quote:
It looks like it already gets ALL the contents of the files it reads, and puts them into report.txt?

Andy - thanks but no the one I posted earlier does not get all contents from each file - just seems to get only the first-line from each file. Can you help? Thanks
Quote Reply
Re: [socrates] Help with this script - open and print data from multiple files In reply to
Hi,

Try this version:

Code:
#!/usr/bin/perl
# here we go ho ho

&read_file("/home/bizspi/www/cgi-bin/logs","report.txt");

sub read_file{

my ($directory, $yourfilename) = @_;
opendir (DIR, $directory) or die ("Can't open dir: '$directory'.\nReason: $!");
my @ls = readdir(DIR);
closedir (DIR);

my $fullfile;
my (@total_list,$total,$tmp);

open (DATAWRITE,">$directory/$yourfilename") or die("Can't open write to file: '$directory/$yourfilename'.\nReason: $!");
foreach $file (@ls){
$fullfile = "$directory/$file";
if (-f $fullfile) {
open (DATA,"$fullfile") or die("Can't open file: '$directory/$fullfile'.\nReason: $!");
while (<DATA>) {
print DATAWRITE $_;
}
close DATA;
}
}
close DATAWRITE;


}

Hope that helps

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: May 11, 2008, 1:17 AM
Quote Reply
Re: [socrates] Help with this script - open and print data from multiple files In reply to
You are using:

Code:
$lines = <DATA>

....which is trying to assign the file contents to a scalar which is why you only get the first line.

It is better to use a while loop to reduce the amount of memory needed.

See Andy's example, but change the file writing lines to:

print DATAWRITE while (<DATA>);

Last edited by:

Wychwood: May 11, 2008, 1:17 AM