Hello there,
i am new on perl and i have this code :
##############################
#!/usr/bin/perl -w
#call of CPAN
use warnings;
use strict;
use Cwd;
#variables
my $i=0;
my $directory= getcwd;
my $file="options";
#opening output file and adding the header on first row
open(FILE, ">>OLTP.txt") or die ("Could not create file OLTP.txt")
+;
print FILE "User script,Serveur Name,Instance Name,Date of script,
+Serveur Name2,Instance Name2,ADVANCED_COMPRESSION~HEADER,TABLE_COMPRE
+SSION~HEADER,2,count,DATA_DICTIONARY_VIEW,TABLE_OWNER,TABLE_NAME,PART
+ITION_NAME,COMPRESSION,COMPRESS_FOR,\n";
close FILE;
#loop while files are found
foreach my $files ( list_files( $directory, 1,$file ) ) {
print "File : $files\n";
singlefile($files,$file);
}
#recursion and list integration
sub list_files {
my ( $directory, $recurse,$file ) = @_;
require File::Spec;
# Search in subdirectory or not
if ( ( not defined $recurse ) || ( $recurse != 1 ) ) { $recurs
+e = 0; }
# verification directory
if ( not defined $directory ) { die "No named directory\n"; }
# Opening a directory
opendir my $fh_rep, $directory or die "Can not open directory
+$directory\n";
# List files and directories, except (. and ..)
my @fic_rep = grep { !/^\.\.?$/ } readdir $fh_rep;
# Closing directory
closedir $fh_rep or die "Unable to close directory $directory\
+n";
#fill list with found files
my @files;
#file or folder? if file: add files to the table. if record: s
+tart the recursion
foreach my $nom (@fic_rep) {
my $mycurrentfile = File::Spec->catdir( $directory, $nom )
+;
if ( -f $mycurrentfile and $mycurrentfile=~ m/$file/ and
+ $mycurrentfile =~ m/\.csv$/i){
push( @files, $mycurrentfile );
}
elsif ( -d $mycurrentfile and $recurse == 1 ) {
push( @files, list_files($mycurrentfile, $recurse,$fil
+e) ); # recursion
}
}
return @files;
}
##merge data after filtering
sub singlefile {
my ( $file,$out) = @_;
#open file
open(FILE, $file) or die ("error occured while opening file");
#create list from file
my @save = <FILE>;
close(FILE);
#empty table rows which do not meet criteria
foreach (@save){
$_ = "" unless ($_ =~ m/"ENABLED","OLTP",/ && $_ =~m/^GREP
+/ );
$_ = "" if ($_ =~m/SYSMAN/|| m/SYS/);
chomp $_;
}
#open output file, add data, close
open(FILE, ">>OLTP.txt") or die ("error occured while opening
+file OLTP.txt");
foreach (@save){
print FILE $_."\n" if ($_);
}
close(FILE);
}
the code seems to do the following :
- create txt with header
- create list of files based on every files that match with criteria (option, csv)
- for each files of the list, fill with all rows and then remove what do not match with the criteria "unless" and "if"
- push everything into the file with header (oltp.txt)
My Goal :
- create txt with header
- create list of file based on every files that match with criteria (option, csv)
- for each files of the list, **fill only with the First rows that match with the criteria "unless" and "if"**
- push everything in the file with header (oltp.txt). the final result should be the txt with header and then, only 1 line per files (if the criteria match).
many thank for your help
OliverR