2014-03-18 83 views
-1

道歉,如果这是一个长长的啰嗦,我真的很感谢这里的答案,因为我很难得到这个工作。perl +读取多个csv文件+操作文件+提供output_files

从这个问题构建here,我有这个脚本在csv文件(orig.csv)上工作,并提供我想要的csv文件(format.csv)。我想要的是使其更通用,并接受任意数量的'.csv'文件,并为每个输入文件提供'output_ csv'。谁能帮忙?

#!/usr/bin/perl 

    use strict; 
    use warnings; 

    open my $orig_fh, '<', 'orig.csv' or die $!; 
    open my $format_fh, '>', 'format.csv' or die $!; 

    print $format_fh scalar <$orig_fh>; # Copy header line 

    my %data; 
    my @labels; 

    while (<$orig_fh>) { 
     chomp; 
     my @fields = split /,/, $_, -1; 
     my ($label, $max_val) = @fields[1,12]; 
     if (exists $data{$label}) { 
     my $prev_max_val = $data{$label}[12] || 0; 
     $data{$label} = \@fields if $max_val and $max_val > $prev_max_val; 
     } 
     else { 
     $data{$label} = \@fields; 
     push @labels, $label; 
     } 
    } 

    for my $label (@labels) { 
     print $format_fh join(',', @{ $data{$label} }), "\n"; 
    } 

我希望能使用来自here这个脚本但我有很大的困难,把2一起:

#!/usr/bin/perl 
    use strict; 
    use warnings; 
    #If you want to open a new output file for every input file 
    #Do it in your loop, not here. 
    #my $outfile = "KAC.pdb"; 
    #open(my $fh, '>>', $outfile); 
    opendir(DIR, "/data/tmp") or die "$!"; 
    my @files = readdir(DIR); 
    closedir DIR; 
    foreach my $file (@files) { 
    open(FH, "/data/tmp/$file") or die "$!"; 
    my $outfile = "output_$file"; #Add a prefix (anything, doesn't have to say 'output') 
    open(my $fh, '>', $outfile); 
    while (<FH>) { 
    my ($line) = $_; 
    chomp($line); 
    if ($line =~ m/KAC 50/) { 
    print $fh $_; 
    } 
    } 
    close($fh); 
    } 

脚本读取目录中的所有文件,并找到符合这个字符串“ KAC 50',然后将该行附加到该inputfileoutput_$file。所以会有1 output_$file对于每个被读这个剧本,我已经注意到

问题inputfile并一直在寻找解决: - 它会读取“”和'..'文件并产生一个 'output_'。和'output_ ..'文件 - 它也会对此脚本文件执行相同的操作。

我还试图使它的动态通过获取这个脚本在它通过添加该代码在任意目录下工作:

use Cwd qw(); 
my $path = Cwd::cwd(); 
print "$path\n"; 

opendir(DIR, $path) or die "$!"; # open the current directory 
open(FH, "$path/$file") or die "$!"; #open the file 

**编辑::我曾尝试结合版本,但我得到errors.Advise不胜感激*

[email protected] ~/Perl 
$ perl formatfile_QforStackOverflow.pl 
Parentheses missing around "my" list at formatfile_QforStackOverflow.pl line 13. 
source dir -> /home/UserName/Perl 
Can't use string ("/home/UserName/Perl/format_or"...) as a symbol ref while "strict refs" in use at formatfile_QforStackOverflow.pl line 28. 

组合代码::

use strict; 
    use warnings; 
    use autodie; # this is used for the multiple files part... 

    #START::Getting current working directory 
    use Cwd qw(); 
    my $source_dir = Cwd::cwd(); 
    #END::Getting current working directory 

    print "source dir -> $source_dir\n"; 
    my $output_prefix = 'format_'; 

    opendir my $dh, $source_dir; #Changing this to work on current directory; changing back 

    for my $file (readdir($dh)) { 
     next if $file !~ /\.csv$/; 
     next if $file =~ /^\Q$output_prefix\E/; 

     my $orig_file = "$source_dir/$file"; 
     my $format_file = "$source_dir/$output_prefix$file"; 

     # .... old processing code here ... 
     ## Start:: This part works on one file edited for this script ## 
     #open my $orig_fh, '<', 'orig.csv' or die $!; #line 14 and 15 above already do this!! 
     #open my $format_fh, '>', 'format.csv' or die $!; 

     #print $format_fh scalar <$orig_fh>; # Copy header line #orig needs changeing 
     print $format_file scalar <$orig_file>; # Copy header line 

     my %data; 
     my @labels; 

     #while (<$orig_fh>) { #orig needs changing 
     while (<$orig_file>) { 
      chomp; 
      my @fields = split /,/, $_, -1; 
      my ($label, $max_val) = @fields[1,12]; 
      if (exists $data{$label}) { 
      my $prev_max_val = $data{$label}[12] || 0; 
      $data{$label} = \@fields if $max_val and $max_val > $prev_max_val; 
      } 
      else { 
      $data{$label} = \@fields; 
      push @labels, $label; 
      } 
     } 

     for my $label (@labels) { 
      #print $format_fh join(',', @{ $data{$label} }), "\n"; #orig needs changing 
      print $format_file join(',', @{ $data{$label} }), "\n"; 
     } 
     ## END:: This part works on one file edited for this script ## 

    } 

回答

0

你打算如何在输入文件处理和他们首选的输出目的地的名单?也许只是有一个固定的目录,你想要处理所有的CVS文件,并以结果为前缀。

#!/usr/bin/perl 

use strict; 
use warnings; 
use autodie; 

my $source_dir = '/some/dir/with/cvs/files'; 
my $output_prefix = 'format_'; 

opendir my $dh, $source_dir; 
for my $file (readdir($dh)) { 
    next if $file !~ /\.csv$/; 
    next if $file =~ /^\Q$output_prefix\E/; 

    my $orig_file = "$source_dir/$file"; 
    my $format_file = "$source_dir/$output_prefix$file"; 

    .... old processing code here ... 

} 

或者,您可以只有一个输出目录而不是前缀文件。无论哪种方式,这应该让你在路上。

+0

对不起,我认为这很清楚,我只是将格式化的文件保存在同一目录中,但他们会有一个前缀来区分。这是否是一种好的做法? tks – HattrickNZ

+0

只要你正确地编写代码,这是一个很好的练习。我所展示的你将完成大部分工作。你最终需要学习更多的编程技巧,而不是把论坛上其他人提供的代码拼凑在一起。祝你好运。 – Miller

+0

仍然出现错误,将不胜感激您的建议,编辑我的Q,tks – HattrickNZ