如何在现有程序中修改“for”循环

时间:2018-06-07 08:35:06

标签: perl

我想修改此代码

#!/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_COMPRESSION~HEADER,2,count,DATA_DICTIONARY_VIEW,TABLE_OWNER,TABLE_NAME,PARTITION_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 ) ) {
        $recurse = 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: start 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, $file ) );    # 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 );
}

似乎做了以下

  1. 使用标题

  2. 创建txt
  3. 根据符合条件的每个文件(选项,csv)创建文件列表

  4. 对于列表中的每个文件,请填写所有行,然后删除与条件unlessif

  5. 不匹配的内容
  6. 使用标题(oltp.txt)

  7. 将所有内容推送到文件中

    我的目标:

    1. 使用标题

    2. 创建txt
    3. 根据与条件匹配的每个文件(选项,csv)创建文件列表

    4. 对于列表中的每个文件, 仅填充符合条件unlessif <的第一行/ p>

    5. 使用标题oltp.txt将所有内容推送到文件中。最终结果应该是带有标题的文本,如果条件匹配,则每个文件最多一行。

0 个答案:

没有答案