Highest value matrix parsing

Hi All

I do have a matrix in the following format

   a_2  a_3 s_4 t_6
b  0 0.9 0.004   0
c 0 0 1 0
d 0 0.98 0 0
e 0.0023 0.96 0 0.0034

I have thousands of rows

I would like to parse the maximum value in each of the row and out put that highest value along the column header of that value. Following is the desired output file


b a_3 0.9
c s_4 1
d a_3 0.98
e a_3 0.96

Please let me know how to do this parsing using awk or sed

Any attempts from your side?

---------- Post updated at 16:42 ---------- Previous update was at 16:35 ----------

Howsoever, try

awk '
NR==1   {for (i=1; i<=NF; i++) TT[i+1]=$i
         next
        }
        {MAX=$2
         COL=2
         for (i=3; i<=NF; i++) if ($i>MAX)      {MAX=$i
                                                 COL=i
                                                }
         print $1, TT[COL], MAX
        }
' file
b a_3 0.9
c s_4 1
d a_3 0.98
e a_3 0.96

The shell fanatic is back. Somebody should time these examples (and maybe one using perl, also):

$ cat foodata
X a_2 a_3 s_4 t_6
b 0 0.9 0.004 0
c 0 0 1 0
d 0 0.98 0 0
e 0.0023 0.96 0 0.0034

$ ./foo.sh <foodata
b: a_3 0.9 (2)
c: s_4 1 (3)
d: a_3 0.98 (2)
e: a_3 0.96 (2)

$ cat foo.sh
#!/bin/bash
read header
columns=($header)
while read data; do
    line=($data)
    max=1 # index of first element
    for (( i=2 ; $i < ${#line[@]}; i++ )) ; do 
    if (( "$(echo ${line[$i]} '>' ${line[$max]} | bc -l)" == "1" )); then max=$i; fi
    done
  echo ${line[0]}: ${columns[$max]} ${line[$max]} "($max)"
done

Notes:

  1. I made a change to the first line of the datafile, so the headings lined up.
  2. Remove the last "($max)" if you don't need the index of the largest element

This is a fun exercise. Anyone want one in, like, SNOBOL?

---------- Post updated at 12:44 PM ---------- Previous update was at 12:30 PM ----------

I have 'way too much free time.

$ ./foo.pl <foodata
b    a_3    0.9
c    s_4    1
d    a_3    0.98
e    a_3    0.96

$ cat foo.pl
#!/usr/bin/perl -w
use strict;
my $debug=1;
my @headings=split ' ',<>;
while (<>) {
    chomp;
    my @line=split ' ',$_;
    my $max=1;
    foreach my $index ( 2 .. $#line ) { $max=$index if ($line[$max] < $line [$index]); }
    print $line[0]."\t".$headings[$max]."\t".$line[$max]."\n";
}