当前位置: 动力学知识库 > 问答 > 编程问答 >

Read specific column in perl

问题描述:

I'm new in perl. I have below text file and from there I want only one Time column and next columns are values. How can I create a text file with my desire output in 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

I've tried below code:

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);

}

网友答案:

I'd go with 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";
}
网友答案:

This is a one-liner; no need to write a script:

$ perl -lanE '$,="\t"; say @F[0,1,3,5]' 1.txt 2.txt 3.txt

If you like, you can shorten it to:

$ perl -lanE '$,="\t"; say @F[0,1,3,5]' [123].txt
网友答案:

Right now, you're just concatenating the lines of the files together. If that doesn't give you the output you like, you need to chop some columns out.

Since your output looks like you have tab delimited files as input, I split the lines coming in by tabs. And since you only wanted the second column, I only take the column at the first offset from the split.

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);
}

Of course, there is no reason to do custom coding to split delimited columns:

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 );
    ...
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: