我是perl的新人。我有下面的文本文件,从那里我只想要一个Time
列,下一列是值。如何在perl中创建带有我的欲望输出的文本文件。
Time Value Time Value Time Value
1 0.353366497 1 0.822193251 1 0.780866396
2 0.168834182 2 0.865650713 2 0.42429447
3 0.323540698 3 0.865984245 3 0.856875894
4 0.721728497 4 0.634773162 4 0.563059042
5 0.545131335 5 0.029808531 5 0.645993399
6 0.143720835 6 0.949973296 6 0.14425803
7 0.414601876 7 0.53421424 7 0.826148814
8 0.194818367 8 0.942334356 8 0.837107013
9 0.291448263 9 0.242588271 9 0.939609775
10 0.500159997 10 0.428897293 10 0.41946448
我尝试过以下代码:
use strict;
use warnings;
use IO::File;
my $result;
my @files = (q[1.txt],q[2.txt],q[3.txt]);
my @fhs = ();
foreach my $file (@files) {
my $fh = new IO::File $file, O_RDONLY;
push @fhs, $fh if defined $fh;
}
while(1) {
my @lines = map { $_->getline } @fhs;
last if grep { not defined $_ } @lines[0..(@fhs-1)];
my @result=join(qq[\t], map { s/[\r?\n]+/ /g; $_ } @lines ) . qq[\r\n];
open (MYFILE, '>>Result.txt');
print (MYFILE "@result");
close (MYFILE);
}
答案 0 :(得分:1)
我会选择split
。
use warnings;
use strict;
open (my $f, '<', 'your-file.dat') or die;
while (my $line = <$f>) {
my @elems = split ' ', $line;
print join "\t", @elems[0,1,3,5];
print "\n";
}
答案 1 :(得分:1)
这是一个单行;无需编写脚本:
$ perl -lanE '$,="\t"; say @F[0,1,3,5]' 1.txt 2.txt 3.txt
如果您愿意,可以将其缩短为:
$ perl -lanE '$,="\t"; say @F[0,1,3,5]' [123].txt
答案 2 :(得分:0)
现在,您只是将文件的行连接在一起。如果它没有给你你喜欢的输出,你需要删除一些列。
由于您的输出看起来像是以制表符分隔的文件作为输入,因此我将按标签分割来的行。由于您只需要第二列,因此我只在拆分的第一个偏移处获取列。
my $line_num = 0;
while(1) {
my @lines = map { $_->getline } @fhs;
last if grep { not defined $_ } @lines[0..$#fhs];
$line_num++;
my @rows = map { [ split /\t/ ] } @lines;
my $time_val = $rows[0][0];
die "Time values are not all equal on line #$line_num!"
if grep { $time_val != $_->[0] } @rows
;
my $result = join( q[\t], $time_val, map { $_->[1] } @rows );
open (MYFILE, '>>Result.txt');
print (MYFILE "$result\n");
close (MYFILE);
}
当然,没有理由进行自定义编码来拆分分隔列:
use Text::CSV;
...
my $csv = Text::CSV->new( { sep_char => "\t" } );
while(1) {
my @rows = map { $csv->getline( $_ ) } @fhs;
last if grep { not defined $_ } @rows[0..$#fhs];
my ( $time_val, @time_vals ) = map { $_->[0] } @rows;
my @values = map { $_->[1] } @rows;
die "Time values are not all equal on line #$line_num!"
if grep { $time_val != $_ } @time_vals
;
my $result = join( q[\t], $time_val, @values );
...
}
答案 3 :(得分:-1)
use strict;
use warnings;
open(FH,"<","a.txt");
print "=========== A File content =========== \n";
my $a = `cat a.txt`;
print "$a\n";
my @temp = <>;
my (@arr, @entries, @final);
foreach ( @temp ) {
@arr = split ( " ", $_ );
push @entries, @arr;
}
close FH;
my @entries1 = @entries;
for(my $i = 7; $i<=$#entries; $i=$i+2) {
push @final, $entries[$i];
}
my $size = scalar @final;
open FH1, ">", "b.txt";
print FH1 "Time \t Value\n";
for(my $i = 0; $i < $size; $i++) {
my $j = $i+1;
print FH1 "$j \t $final[$i]\n";
}
close FH1;
print "============ B file content ===============\n";
my $b = `cat b.txt`;
print "$b";
O/P:
=========== A File content ===========
Time Value Time Value Time Value
1 0.353366497 1 0.822193251 1 0.780866396
2 0.168834182 2 0.865650713 2 0.42429447
3 0.323540698 3 0.865984245 3 0.856875894
4 0.721728497 4 0.634773162 4 0.563059042
5 0.545131335 5 0.029808531 5 0.645993399
6 0.143720835 6 0.949973296 6 0.14425803
7 0.414601876 7 0.53421424 7 0.826148814
8 0.194818367 8 0.942334356 8 0.837107013
9 0.291448263 9 0.242588271 9 0.939609775
10 0.500159997 10 0.428897293 10 0.41946448
============ B file content ===============
Time Value
1 0.353366497
2 0.822193251
3 0.780866396
4 0.168834182
5 0.865650713
6 0.42429447
7 0.323540698
8 0.865984245
9 0.856875894
10 0.721728497
11 0.634773162
12 0.563059042
13 0.545131335
14 0.029808531
15 0.645993399
16 0.143720835
17 0.949973296
18 0.14425803
19 0.414601876
20 0.53421424
21 0.826148814
22 0.194818367
23 0.942334356
24 0.837107013
25 0.291448263
26 0.242588271
27 0.939609775
28 0.500159997
29 0.428897293
30 0.41946448