#!/usr/bin/perl -l use warnings; use strict; # get the header into an array my @header = split /\s+/, ; # conjure a regex using the name of columns you wanted # which is the array you have my $to_find = join( '|' => qw(nat pls kac) ); my $reg_to_use = qr/$to_find/; # get the index of the header you need my @index = grep { $header[$_] =~ /$reg_to_use/ } 0 .. $#header; print join( "\t" => @header[@index] ); while () { print join( "\t" => ( split /\s+/, $_ )[@index] ); } __DATA__ nat pls fof tri pls fof tri kac 0.1 0.1 0.23 0.1 0.1 0.23 0.1 0.31 2.3 1.8 3.2 4.4 1.8 3.2 4.4 3.21 5.5 3.2 8.6. 7.9 3.2 8.6. 7.9 2.89 #### nat pls pls kac 0.1 0.1 0.1 0.31 2.3 1.8 1.8 3.21 5.5 3.2 3.2 2.89