Without an example of your input file this is my best guess

updated with sample data from Re^2: Data extraction with specific keywords
#!/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
poj

In reply to Re: Data extraction with specific keywords by poj
in thread Data extraction with specific keywords by neeraj_kr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.