0

I have space-separated file with 4 columns. I want to make use of awk to find out max from each column. My awk script should output that information at end of file.

e.g if my file is like this:

              Banana  Oranges  Lemons 
Case_1        50      243      143
Case_2        45      443      103
Case_3        56      234      128
Case_4        64      164      183
Case_5        54      342      176

After running awk script it should output,

              Banana  Oranges  Lemons 
Case_1        50      243      143
Case_2        45      443      103
Case_3        56      234      128
Case_4        64      164      183
Case_5        54      342      176

Banana maximum happens at case 4: The complete line is
Case_4        64      164      183

Orange maximum happens at case 2: The complete line is
Case_2        45      443      103

Can someone help me with awk code?

3
  • 4
    John, what have you tried?
    – JJoao
    Feb 11, 2017 at 8:41
  • You are expected to show your own code and explain where you are stuck. You should not expect someone to do your work for free. Feb 11, 2017 at 13:43
  • 1
    This is not a CSV file. The first column is header-less and the delimiter is not fixed. It's just a table in text form.
    – Kusalananda
    Feb 11, 2017 at 22:24

1 Answer 1

1
BEGIN {
 col[0]=""
 max[0]=""
 casenum=0
 text[0]=""
}

{ print $0 }

NR == 1 { split($0,col,FS); }

/Case_/ && NR > 1 { 
  casenum++
  split($0,vals,FS)
  text[casenum]=$0
  for(i=1;i<=length(col);i++)
  { 
    if(vals[i+1] > max[i])
    {
      max[i]=vals[i+1]
      item[i]=casenum
    }
  }
}

END {
  for(i=1;i<=length(col);i++)
  {
    printf "\n%s maximum happens at case %d: The complete line is\n",col[i],item[i]
    print text[item[i]]
  }
}

This produces the output:

              Banana  Oranges  Lemons 
Case_1        50      243      143
Case_2        45      443      103
Case_3        56      234      128
Case_4        64      164      183
Case_5        54      342      176

Banana maximum happens at case 4: The complete line is
Case_4        64      164      183 

Oranges maximum happens at case 2: The complete line is
Case_2        45      443      103 

Lemons maximum happens at case 4: The complete line is
Case_4        64      164      183 
2
  • Thanks a lot Chunko. Your solution does work. I was thinking about making use of sort utility as awk '{for(i=1;i<=NF;i++) if($i ~/^[0-9]+/) $i=$i*1; print $0 > "temp_file";} END {command = "sort -nk2 temp_file"; max=command; print $max}' input.csv as of now print $max only print last line from the table. I thought of making use of sort utility to sort table column-wise and print min and max case. but I am not getting whole table in "max" variable.( If I could get it, I can split it as per \n and then print 1st and last element of the array). Does anyone understand where im going wrong ?
    – John
    Feb 13, 2017 at 5:55
  • If the above does what you asked can you mark the answer accepted? I'm trying to build enough points so that I can comment and stuff. I'm not sure about your sorting question it sounds like its unrelated? If so maybe start a new question so you can explain it longer
    – Chunko
    Feb 13, 2017 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .