file 1 col0 col1 col2 col3 col4 col5 ... ... samp1 samp2 samp3 samp4 .... files follow similar format and what i need to do is extract 1st and 4th column from the first file and output on a new file and then take out only 4th column from the rest of the files. So before i work on my actual project, i wanted to try with more simple table. The table i'm working on right now is S1.txt col1 col2 col3 1 4 7 2 5 8 3 6 9 S2.txt col1 col2 col3 1 44 77 2 55 88 3 66 99 The result i'm getting col1 col3 col3 1 4 2 5 3 6 The result i want col1 col3 col3 1 4 77 2 5 88 3 6 99 #### #!/usr/bin/perl -w use warnings; use strict; my @desired_cols = qw(colname1 colname3); my @desired_cols1= qw(colname3); my $temp = 'tmp.txt'; # reads first line to get actual column names open(S1, 'S1.txt') || die "Can't open S1: $!"; open(S2, 'S2.txt') || die "Can't open S2 : $!"; open(OUT, ">$temp") || die "Can't create output : $!"; my $header_line = (); my $header_line1 = (); my @actual_cols = split(/\s+/,$header_line); # get column number of the actual column names my $pos =0; my %col2_num = map {$_ => $pos++}@actual_cols; # translate the desired col names into position numbers my @slice = map{$col2_num{$_}}@desired_cols; my @slice1 = map{$col2_num{$_}}@desired_cols1; print OUT join("\t","@desired_cols"),"\r\n"; #header line # print colname1 colname3 colname3 in outfile while (, ) { my @row = (split)[@slice]; my @row1 = (split)[@slice1]; print OUT join("\t","@row @row1"),"\r\n"; #each data row }