我有一個看起來像這樣的數據:創建文件迅速從單個列
-1 1:-0.394668 2:-0.794872 3:-1 4:-0.871341 5:0.9365 6:0.75597
1 1:-0.463641 2:-0.897436 3:-1 4:-0.871341 5:0.44378 6:0.121824
1 1:-0.469432 2:-0.897436 3:-1 4:-0.871341 5:0.32668 6:0.302529
-1 1:-0.241547 2:-0.538462 3:-1 4:-0.871341 5:0.9994 6:0.987166
1 1:-0.757233 2:-0.948718 3:-1 4:-0.871341 5:-0.33904 6:0.915401
1 1:-0.167147 2:-0.589744 3:-1 4:-0.871341 5:0.95078 6:0.991566
第一列是一流的,而接下來的6列功能。我想爲個別功能創建6個文件 。例如
my_input_feat1.txt將包含
-1 1:-0.394668
1 1:-0.463641
...
1 1:-0.757233
1 1:-0.167147
my_input_feat2.txt將包含
-1 2:-0.794872
...
1 2:-0.589744
等。我有一個Perl代碼,但它的速度非常慢。有沒有 更快的方法?通常輸入文件將包含100K行。
use strict;
use Data::Dumper;
use Carp;
my $input = $ARGV[0] || "myinput.txt";
my $INFILE_file_name = $input; # input file name
open (INFILE, '<', $INFILE_file_name)
or croak "$0 : failed to open input file $INFILE_file_name : $!\n";
my $out1 = $input."_feat_1.txt";
my $out2 = $input."_feat_2.txt";
my $out3 = $input."_feat_3.txt";
my $out4 = $input."_feat_4.txt";
my $out5 = $input."_feat_5.txt";
my $out6 = $input."_feat_6.txt";
unlink($out1);
unlink($out2);
unlink($out3);
unlink($out4);
unlink($out5);
unlink($out6);
print "$out1\n";
while (<INFILE>) {
chomp;
my @els = split(/\s+/,$_);
my $lbl = $els[0];
my $OUTFILE1_file_name = $out1; # output file name
open (OUTFILE1, '>>', $OUTFILE1_file_name)
or croak "$0 : failed to open output file $OUTFILE1_file_name : $!\n";
print OUTFILE1 "$lbl $els[1]\n";
close (OUTFILE1); # close output file
my $OUTFILE2_file_name = $out2; # output file name
open (OUTFILE2, '>>', $OUTFILE2_file_name)
or croak "$0 : failed to open output file $OUTFILE2_file_name : $!\n";
print OUTFILE2 "$lbl $els[2]\n";
close (OUTFILE2); # close output file
# Etc.. until OUTFILE 6
}
close (INFILE);
謝謝。但我想有一個輸入名稱的變量。這樣我可以根據不同的輸入名稱動態創建這樣的文件。請注意,功能的數量可以大於/小於6. – neversaint 2011-01-11 07:55:53
@neversaint:將上面的文件名抽象出來放到shell變量中,並將其全部放入Bash腳本中應該很容易。只需將data.txt替換爲$(FILENAME),並確保在調用awk之前設置FILENAME。 – unwind 2011-01-11 07:58:29