Home: Perl Programming Help: Beginner:
ready files into array and collect from yesterday



jeffersno1
Novice

Jul 23, 2012, 8:39 PM


Views: 2323
ready files into array and collect from yesterday

Morning all,

I'm starting to pull my hair out with a script that logs onto a production server looks for files for yesterday and copies them back into 1 file on the local machine, sounds easy huh.

Now i want to store the file locally and then run a perl script to obtain stats from it. I can do all the stats part just cant gather the file(s). Most of the time there is only 1 file but when systems crash there are multiple.

Here is my attempt


Code
#!/usr/bin/perl 

use warnings;
use POSIX 'strftime';
my $remo_main_dir = '/export/home/user';
my $loc_main_dir = '/home/system_prod';
my $logs_dir = "$remo_main_dir/prod/app_logs";

my $date = strftime("%Y%m%d", localtime(time - 86400));
print $date;

@results1 = command_to_array("/usr/bin/ssh user\@prod1 '/bin/ls /$logs_dir/event_log_$date??????.file'");

foreach (@results1){
s/\/\//\//g;
print "$_\n";}

sub command_to_array ## given from a friend :)
{
my $command=shift;
my $CMD="";
my @outdata=();

# print "CMD - {$command}\n";

open (CMD, "$command|") or die "FAILED to open file!";
while (<CMD>)
{
$line=$_;
chomp($line);
push(@outdata,$line);
}
close (CMD);

return @outdata;
}


Now the print works well, i see files for yesterday buy cant copy them to my local machine. Is there an easier way to do this?, i think im making this much more complicated than it needs to be.

Thanks in advance

Jeffers


rovf
Veteran

Jul 24, 2012, 1:17 AM


Views: 2317
Re: [jeffersno1] ready files into array and collect from yesterday

In the code you posted, I can't see that you are copying a file at all. Maybe you didn't post the complete code?

BTW, just being curious: Is there a reason why you don't simply write


Code
@results1 = qx(/usr/bin/ssh user\@prod1 '/bin/ls /$logs_dir/event_log_$date??????.file');


and use command_to_array instead? If you really need to chomp (but I don't think you have to, since you are appending a \n afterwards), you can easily do a


Code
chomp(@results1)


afterwards.


jeffersno1
Novice

Jul 24, 2012, 2:57 PM


Views: 2225
Re: [rovf] ready files into array and collect from yesterday

You see this is where i over complicate things.

i'm listing the files into an array then using the array to collect the files. so really im connecting to the server twice to get the just collect the files..

Has anyone got a suggestion how i can search a given directory and copy files from the day before into one files on the local machine ?

Many thanks for your help, its much appreciated.

Jeffers


rovf
Veteran

Jul 25, 2012, 1:34 AM


Views: 2129
Re: [jeffersno1] ready files into array and collect from yesterday

scp ?


jeffersno1
Novice

Jul 25, 2012, 4:22 AM


Views: 2113
Re: [jeffersno1] ready files into array and collect from yesterday

Hi,

I've used scp. But that still requires connecting to the server twice!!

Once to find out the files and put them into the array and another to scp.

Thanks