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;
}
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;
}