in reply to Data extraction with specific keywords
Without an example of your input file this is my best guess
updated with sample data from Re^2: Data extraction with specific keywordspoj#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @colname = ("2:", "6:", "4:", "3:"); my $datafile = 'datafile2.txt';#$ARGV[0]; open my $fh,'<',$datafile or die "Could not open $datafile : $!"; # column header line my @header = split /\s+/,<$fh>; chomp(@header); #print Dumper \@header; # convert name to colno my %colno = (); for my $i (0..$#header){ my ($name,$val) = split ':',$header[$i]; $colno{ $name.':' } = $i; } #print Dumper \%colno; # array of required columns my @colno = map { $colno{$_} } @colname; #print Dumper \@colno; # process datalines seek $fh,0,0; while (<$fh>){ chomp; my @f = split /\s+/,$_; print join "\t",@f[@colno]; print "\n"; } close $fh; __DATA__ 0 2:-0.5795 3:0.33582025 4:55.8255 5:65.316997 6:15 7:16 8:57 9:28 10: +29 11:23 12:5 1 2:-1.4034 3:1.96953156 4:53.4424 5:65.316997 6:15 7:16 8:57 9:28 10: +29 11:23 12:5
|
|---|