perlnovice1900 has asked for the wisdom of the Perl Monks concerning the following question:

Hello kind Monks, I am having difficulty pulling data from inside multiple log files and sorting that data into one singular file that consists of every file.

I have created a perl script that can take all of the files in any directory and put the ones that are wanted into an array, (in this case I want all of the log files that start with 'secure'). I can get the files into the array no problem, but I do not know how to condense all of the data into a singular file afterwards.

Here is my code:

my @JanFiles; my $file; my $infile; my $JanFiles; my @files; my $dh; my @secures; my $outfile = 'secure.200601'; my $INFILE = @secures; opendir( $dh, "." ) or die "Can't open '.':$!\n"; while( $file = readdir($dh) ) { # add it to the set of files... push( @files, $file ); #print "[$file]\n"; } closedir( $dh ); # verify that @files got stuff .. print "===========================\n"; print "Verifying all file names.\n"; foreach( @files ) { print "[$_]\n"; } # Let's make a private reserve # of files that contain the word # 'secure' in file name #@secures; foreach( @files ) { if (/secure/) { if ( $_ ne "secure.200601") { push( @secures, $_ ); } } } #@secures = sort @secures; print "===========================\n"; print "Verifying only 'secure' file names.\n"; foreach( @secures ) { print "secure file = [$_]\n" } foreach( @secures ) { push (@secures, grep(/^Jan/ , @JanFiles )); } foreach( @JanFiles ) { print "JAN FILES = [$_]\n" } open (FILE, ">> $outfile") || die "Unable to open Write File! \n"; print FILE @JanFiles; close FILE; #foreach( @JanFiles ) { # print "JanFile = [$_]\n" #} exit;

Thank you in advance. I will be here for any further needed information.

  • Comment on How to output file data from files in an array to one singular file?
  • Download Code

Replies are listed 'Best First'.
Re: How to output file data from files in an array to one singular file?
by huck (Prior) on Mar 26, 2017 at 00:11 UTC

    You have gotten close. I bet you noticed that $outfile only contained a list of the filenames that were janfiles, rather than the contents of those files.

    open (my $ofile, '>',$outfile) || die "Unable to open Write File! $out +file\n"; for my $infn (@JanFiles) { open (my $infile,'<',$infn) || die "Unable to open input file! $infi +le \n"; while (my $line=<$infile>) { print $ofile $line;} close ($infile); } close $ofile;

    Edit, fixed quotes typo from c&p

      Hi Huck,

      If I wanted to output that data to a file called 'secure.2006' how would I go about doing that?

      Also big thanks for helping me out!

        It will output to whatever file is named in my $outfile.

        my $outfile = 'secure.200601'; does set it to what you wanted

Re: How to output file data from files in an array to one singular file?
by tybalt89 (Monsignor) on Mar 26, 2017 at 00:04 UTC
    open (FILE, ">> $outfile") || die "Unable to open Write File! \n"; #print FILE @JanFiles; do { local @ARGV = @JanFiles; print FILE $_ while <> }; close FILE;

    untested...