How to extract column name (header) from a CSV file which contains the max value in a row?How to select rows...

Why was the Ancient One so hesitant to teach Dr. Strange the art of sorcery?

Why do Thanos's punches not kill Captain America or at least cause some mortal injuries?

Can the sorting of a list be verified without comparing neighbors?

Does kinetic energy warp spacetime?

Proof that the inverse image of a single element is a discrete space

Surely they can fit?

How to minimise the cost of guessing a number in a high/low guess game?

What is the significance of 4200 BCE in context of farming replacing foraging in Europe?

Can I use my laptop, which says 100-240V, in the USA?

Early arrival in Australia, early hotel check in not available

Do atomic orbitals "pulse" in time?

Reaction of borax with NaOH

Create a list of all possible Boolean configurations of three constraints

Front derailleur hard to move due to gear cable angle

What happens if a creature that would fight isn't on the battlefield anymore?

How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?

Drawing lines to nearest point

Why was Thor doubtful about his worthiness to Mjolnir?

Page contents aligning weirdly in LaTeX/Overleaf

What food production methods would allow a metropolis like New York to become self sufficient

What is Plautus’s pun about frustum and frustrum?

Exception propagation: When should I catch exceptions?

Plastic-on-plastic lubricant that wont leave a residue?

What's the word for the soldier salute?



How to extract column name (header) from a CSV file which contains the max value in a row?


How to select rows from a CSV file based on different column values?How can I combine different csv files from differenet folders with the name of each file as a column name in the combined file?How to sum column values for each row in two csv files using bash script?Execute Linux command only if column in CSV file has a value greater than 1How to redirect linux command outputs to specific row and column in csv?How to find specific csv files and extract their second row into a single csv fileCopy column with file name as column headerBash script : How to export file name as header to csvNeed to print value from 12th column of a particular row that matches two variables in a CSV fileRemove specific columns from csv using awk






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















I am trying to extract the column name with the maximum value in each row using bash script i.e., the column header value or the value from the same column in the first row. I am using the following to extract the max value from each row in a CSV file but can't find out how to print its column name along with the max value:



awk -F ',' '{max=$'$col1';for (i=1;i<=NF;i++) {if ($i > max){max=$i}};print " max: " max}' "$INPUT_PATH/tmp.csv" >>$INPUT_PATH/max1.csv


Example:



Sample CSV Data:



col1,col2,col3,col4
1,5,2,6
4,0,1,2
1,2,0,0
0,0,7,0


Desired Output:



col4 6 2
col1 4 1
col2 2 2
col3 7 3


Is there a way to do this in the above command or is there a better way to extract the desired information from the CSV file?










share|improve this question
















bumped to the homepage by Community 32 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

    – Ankit Vashistha
    Jul 23 '15 at 8:16






  • 3





    Sample data would be useful I think.

    – Sobrique
    Jul 23 '15 at 9:16




















2















I am trying to extract the column name with the maximum value in each row using bash script i.e., the column header value or the value from the same column in the first row. I am using the following to extract the max value from each row in a CSV file but can't find out how to print its column name along with the max value:



awk -F ',' '{max=$'$col1';for (i=1;i<=NF;i++) {if ($i > max){max=$i}};print " max: " max}' "$INPUT_PATH/tmp.csv" >>$INPUT_PATH/max1.csv


Example:



Sample CSV Data:



col1,col2,col3,col4
1,5,2,6
4,0,1,2
1,2,0,0
0,0,7,0


Desired Output:



col4 6 2
col1 4 1
col2 2 2
col3 7 3


Is there a way to do this in the above command or is there a better way to extract the desired information from the CSV file?










share|improve this question
















bumped to the homepage by Community 32 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
















  • Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

    – Ankit Vashistha
    Jul 23 '15 at 8:16






  • 3





    Sample data would be useful I think.

    – Sobrique
    Jul 23 '15 at 9:16
















2












2








2


1






I am trying to extract the column name with the maximum value in each row using bash script i.e., the column header value or the value from the same column in the first row. I am using the following to extract the max value from each row in a CSV file but can't find out how to print its column name along with the max value:



awk -F ',' '{max=$'$col1';for (i=1;i<=NF;i++) {if ($i > max){max=$i}};print " max: " max}' "$INPUT_PATH/tmp.csv" >>$INPUT_PATH/max1.csv


Example:



Sample CSV Data:



col1,col2,col3,col4
1,5,2,6
4,0,1,2
1,2,0,0
0,0,7,0


Desired Output:



col4 6 2
col1 4 1
col2 2 2
col3 7 3


Is there a way to do this in the above command or is there a better way to extract the desired information from the CSV file?










share|improve this question
















I am trying to extract the column name with the maximum value in each row using bash script i.e., the column header value or the value from the same column in the first row. I am using the following to extract the max value from each row in a CSV file but can't find out how to print its column name along with the max value:



awk -F ',' '{max=$'$col1';for (i=1;i<=NF;i++) {if ($i > max){max=$i}};print " max: " max}' "$INPUT_PATH/tmp.csv" >>$INPUT_PATH/max1.csv


Example:



Sample CSV Data:



col1,col2,col3,col4
1,5,2,6
4,0,1,2
1,2,0,0
0,0,7,0


Desired Output:



col4 6 2
col1 4 1
col2 2 2
col3 7 3


Is there a way to do this in the above command or is there a better way to extract the desired information from the CSV file?







shell-script awk csv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 25 '15 at 14:28







Ankit Vashistha

















asked Jul 23 '15 at 8:12









Ankit VashisthaAnkit Vashistha

88182130




88182130





bumped to the homepage by Community 32 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community 32 mins ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

    – Ankit Vashistha
    Jul 23 '15 at 8:16






  • 3





    Sample data would be useful I think.

    – Sobrique
    Jul 23 '15 at 9:16





















  • Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

    – Ankit Vashistha
    Jul 23 '15 at 8:16






  • 3





    Sample data would be useful I think.

    – Sobrique
    Jul 23 '15 at 9:16



















Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

– Ankit Vashistha
Jul 23 '15 at 8:16





Along with the column name, how can i print the column value also i.e, if the max value is in column 5, it should print 5.

– Ankit Vashistha
Jul 23 '15 at 8:16




3




3





Sample data would be useful I think.

– Sobrique
Jul 23 '15 at 9:16







Sample data would be useful I think.

– Sobrique
Jul 23 '15 at 9:16












4 Answers
4






active

oldest

votes


















0














Your future self (and anyone else having to maintain the software) will thank you if you use a language like Python for this. Of course it's not going to be a one-liner, but at least it's readable Naive pseudo-code goes something like this (completely untested):



import csv
import defaultdict

with open('max1.csv') as file_handle:
csv_reader = csv.reader(file_handle)
headers = csv_reader.next()
maxes = defaultdict(0) # Or negative infinity
for values in csv_reader:
for index in range(len(values)):
if value > maxes[headers[index]]:
maxes[headers[index]] = value





share|improve this answer
























  • Could you please suggest the way in bash script or the changes in my awk for the same?

    – Ankit Vashistha
    Jul 23 '15 at 8:51



















0














It is a bit unclear what you are asking, I assume you want to print for every row max value of the row and column header for column in wich this value is found:



BEGIN {
FS = ",";
}
NR == 1 {
for (i = 1; i <= NF; i++) {
x[i] = $i;
}
next;
}
{
max = $1 + 0;
for (i = 1; i <= NF; i++) {
if (max <= ($i + 0)) {
v[x[i]] = $i + 0;
max = (v[x[i]] >= max) ? v[x[i]] : max;
}
}

printf("Row %d: Column(s): ", NR);
for (i in v) {
if (max == v[i])
printf("%s ", i);
}
print "max value: " max;
}


You can save above in file.awk and run:



awk -f file.awk your input


So for given input:



col1,col2,col3,col4,col5,col6,col7,col8
-1,-2,-22,-4,-1,-2,-4,-8
-9,-3,-2,-1,-2,-4,-5,-7
0,-3,-2,-1,-10,-11,-2,-8


Output should be:



Row 2, Colums(s): col1 col5 max value: -1
Row 3, Colums(s): col4 col5 max value: -1
Row 4, Colums(s): col1 max value: 0





share|improve this answer

































    0














    The following allows for a repeating maximum on the same line.



    awk -F, 'NR==1 { split($0,head,FS); next }
    { max=0; delete a;
    for(i=1;i<=NF;i++) if($i>=max){ max=$i; a[max]=a[max]head[i]" ("i"), " }
    print "max " max "t" substr(a[max], 0, length(a[max])-2)
    }' file


    input:



    hdr A,hdr B,hdr C,hdr D,hdr E,hdr F
    5,2,7,4,7,-9
    1,5,4,3,2,1
    1,5,9,9,5,3


    output:



    max 7   hdr C (3), hdr E (5)
    max 5 hdr B (2)
    max 9 hdr C (3), hdr D (4)





    share|improve this answer

































      -1














      The problem with CSV is it doesn't parse nicely with normal shell tools. They simply don't do it nicely. It can be done in trivial cases, but really - a scripting language is the tool for the job.



      I'd be thinking more perl personally:



      #!/usr/bin/env perl
      use strict;
      use warnings;
      use Text::CSV;

      my $csv = Text::CSV->new();

      open ( my $input, "<", "your_file.csv" ) or die $!;
      $csv->column_names( $csv->getline( $input ) );

      while ( my $row = $csv->getline_hr( $input ) ) {
      my ( $highest, @rest ) = sort { $row->{$b} <=> $row->{$a} } keys %$row;
      print join( "t", $highest, $row->{$highest} ), "n";
      }


      Which if using as input:



      first,second,third,fourth
      1,3,4,5,
      5,4,3,2,
      1,1,4,1,


      Will print:



      fourth  5
      first 5
      third 4





      share|improve this answer



















      • 1





        "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

        – Wildcard
        Nov 22 '16 at 10:54














      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f217817%2fhow-to-extract-column-name-header-from-a-csv-file-which-contains-the-max-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Your future self (and anyone else having to maintain the software) will thank you if you use a language like Python for this. Of course it's not going to be a one-liner, but at least it's readable Naive pseudo-code goes something like this (completely untested):



      import csv
      import defaultdict

      with open('max1.csv') as file_handle:
      csv_reader = csv.reader(file_handle)
      headers = csv_reader.next()
      maxes = defaultdict(0) # Or negative infinity
      for values in csv_reader:
      for index in range(len(values)):
      if value > maxes[headers[index]]:
      maxes[headers[index]] = value





      share|improve this answer
























      • Could you please suggest the way in bash script or the changes in my awk for the same?

        – Ankit Vashistha
        Jul 23 '15 at 8:51
















      0














      Your future self (and anyone else having to maintain the software) will thank you if you use a language like Python for this. Of course it's not going to be a one-liner, but at least it's readable Naive pseudo-code goes something like this (completely untested):



      import csv
      import defaultdict

      with open('max1.csv') as file_handle:
      csv_reader = csv.reader(file_handle)
      headers = csv_reader.next()
      maxes = defaultdict(0) # Or negative infinity
      for values in csv_reader:
      for index in range(len(values)):
      if value > maxes[headers[index]]:
      maxes[headers[index]] = value





      share|improve this answer
























      • Could you please suggest the way in bash script or the changes in my awk for the same?

        – Ankit Vashistha
        Jul 23 '15 at 8:51














      0












      0








      0







      Your future self (and anyone else having to maintain the software) will thank you if you use a language like Python for this. Of course it's not going to be a one-liner, but at least it's readable Naive pseudo-code goes something like this (completely untested):



      import csv
      import defaultdict

      with open('max1.csv') as file_handle:
      csv_reader = csv.reader(file_handle)
      headers = csv_reader.next()
      maxes = defaultdict(0) # Or negative infinity
      for values in csv_reader:
      for index in range(len(values)):
      if value > maxes[headers[index]]:
      maxes[headers[index]] = value





      share|improve this answer













      Your future self (and anyone else having to maintain the software) will thank you if you use a language like Python for this. Of course it's not going to be a one-liner, but at least it's readable Naive pseudo-code goes something like this (completely untested):



      import csv
      import defaultdict

      with open('max1.csv') as file_handle:
      csv_reader = csv.reader(file_handle)
      headers = csv_reader.next()
      maxes = defaultdict(0) # Or negative infinity
      for values in csv_reader:
      for index in range(len(values)):
      if value > maxes[headers[index]]:
      maxes[headers[index]] = value






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Jul 23 '15 at 8:46









      l0b0l0b0

      29k20122253




      29k20122253













      • Could you please suggest the way in bash script or the changes in my awk for the same?

        – Ankit Vashistha
        Jul 23 '15 at 8:51



















      • Could you please suggest the way in bash script or the changes in my awk for the same?

        – Ankit Vashistha
        Jul 23 '15 at 8:51

















      Could you please suggest the way in bash script or the changes in my awk for the same?

      – Ankit Vashistha
      Jul 23 '15 at 8:51





      Could you please suggest the way in bash script or the changes in my awk for the same?

      – Ankit Vashistha
      Jul 23 '15 at 8:51













      0














      It is a bit unclear what you are asking, I assume you want to print for every row max value of the row and column header for column in wich this value is found:



      BEGIN {
      FS = ",";
      }
      NR == 1 {
      for (i = 1; i <= NF; i++) {
      x[i] = $i;
      }
      next;
      }
      {
      max = $1 + 0;
      for (i = 1; i <= NF; i++) {
      if (max <= ($i + 0)) {
      v[x[i]] = $i + 0;
      max = (v[x[i]] >= max) ? v[x[i]] : max;
      }
      }

      printf("Row %d: Column(s): ", NR);
      for (i in v) {
      if (max == v[i])
      printf("%s ", i);
      }
      print "max value: " max;
      }


      You can save above in file.awk and run:



      awk -f file.awk your input


      So for given input:



      col1,col2,col3,col4,col5,col6,col7,col8
      -1,-2,-22,-4,-1,-2,-4,-8
      -9,-3,-2,-1,-2,-4,-5,-7
      0,-3,-2,-1,-10,-11,-2,-8


      Output should be:



      Row 2, Colums(s): col1 col5 max value: -1
      Row 3, Colums(s): col4 col5 max value: -1
      Row 4, Colums(s): col1 max value: 0





      share|improve this answer






























        0














        It is a bit unclear what you are asking, I assume you want to print for every row max value of the row and column header for column in wich this value is found:



        BEGIN {
        FS = ",";
        }
        NR == 1 {
        for (i = 1; i <= NF; i++) {
        x[i] = $i;
        }
        next;
        }
        {
        max = $1 + 0;
        for (i = 1; i <= NF; i++) {
        if (max <= ($i + 0)) {
        v[x[i]] = $i + 0;
        max = (v[x[i]] >= max) ? v[x[i]] : max;
        }
        }

        printf("Row %d: Column(s): ", NR);
        for (i in v) {
        if (max == v[i])
        printf("%s ", i);
        }
        print "max value: " max;
        }


        You can save above in file.awk and run:



        awk -f file.awk your input


        So for given input:



        col1,col2,col3,col4,col5,col6,col7,col8
        -1,-2,-22,-4,-1,-2,-4,-8
        -9,-3,-2,-1,-2,-4,-5,-7
        0,-3,-2,-1,-10,-11,-2,-8


        Output should be:



        Row 2, Colums(s): col1 col5 max value: -1
        Row 3, Colums(s): col4 col5 max value: -1
        Row 4, Colums(s): col1 max value: 0





        share|improve this answer




























          0












          0








          0







          It is a bit unclear what you are asking, I assume you want to print for every row max value of the row and column header for column in wich this value is found:



          BEGIN {
          FS = ",";
          }
          NR == 1 {
          for (i = 1; i <= NF; i++) {
          x[i] = $i;
          }
          next;
          }
          {
          max = $1 + 0;
          for (i = 1; i <= NF; i++) {
          if (max <= ($i + 0)) {
          v[x[i]] = $i + 0;
          max = (v[x[i]] >= max) ? v[x[i]] : max;
          }
          }

          printf("Row %d: Column(s): ", NR);
          for (i in v) {
          if (max == v[i])
          printf("%s ", i);
          }
          print "max value: " max;
          }


          You can save above in file.awk and run:



          awk -f file.awk your input


          So for given input:



          col1,col2,col3,col4,col5,col6,col7,col8
          -1,-2,-22,-4,-1,-2,-4,-8
          -9,-3,-2,-1,-2,-4,-5,-7
          0,-3,-2,-1,-10,-11,-2,-8


          Output should be:



          Row 2, Colums(s): col1 col5 max value: -1
          Row 3, Colums(s): col4 col5 max value: -1
          Row 4, Colums(s): col1 max value: 0





          share|improve this answer















          It is a bit unclear what you are asking, I assume you want to print for every row max value of the row and column header for column in wich this value is found:



          BEGIN {
          FS = ",";
          }
          NR == 1 {
          for (i = 1; i <= NF; i++) {
          x[i] = $i;
          }
          next;
          }
          {
          max = $1 + 0;
          for (i = 1; i <= NF; i++) {
          if (max <= ($i + 0)) {
          v[x[i]] = $i + 0;
          max = (v[x[i]] >= max) ? v[x[i]] : max;
          }
          }

          printf("Row %d: Column(s): ", NR);
          for (i in v) {
          if (max == v[i])
          printf("%s ", i);
          }
          print "max value: " max;
          }


          You can save above in file.awk and run:



          awk -f file.awk your input


          So for given input:



          col1,col2,col3,col4,col5,col6,col7,col8
          -1,-2,-22,-4,-1,-2,-4,-8
          -9,-3,-2,-1,-2,-4,-5,-7
          0,-3,-2,-1,-10,-11,-2,-8


          Output should be:



          Row 2, Colums(s): col1 col5 max value: -1
          Row 3, Colums(s): col4 col5 max value: -1
          Row 4, Colums(s): col1 max value: 0






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 23 '15 at 16:22

























          answered Jul 23 '15 at 10:44









          taliezintaliezin

          7,02011728




          7,02011728























              0














              The following allows for a repeating maximum on the same line.



              awk -F, 'NR==1 { split($0,head,FS); next }
              { max=0; delete a;
              for(i=1;i<=NF;i++) if($i>=max){ max=$i; a[max]=a[max]head[i]" ("i"), " }
              print "max " max "t" substr(a[max], 0, length(a[max])-2)
              }' file


              input:



              hdr A,hdr B,hdr C,hdr D,hdr E,hdr F
              5,2,7,4,7,-9
              1,5,4,3,2,1
              1,5,9,9,5,3


              output:



              max 7   hdr C (3), hdr E (5)
              max 5 hdr B (2)
              max 9 hdr C (3), hdr D (4)





              share|improve this answer






























                0














                The following allows for a repeating maximum on the same line.



                awk -F, 'NR==1 { split($0,head,FS); next }
                { max=0; delete a;
                for(i=1;i<=NF;i++) if($i>=max){ max=$i; a[max]=a[max]head[i]" ("i"), " }
                print "max " max "t" substr(a[max], 0, length(a[max])-2)
                }' file


                input:



                hdr A,hdr B,hdr C,hdr D,hdr E,hdr F
                5,2,7,4,7,-9
                1,5,4,3,2,1
                1,5,9,9,5,3


                output:



                max 7   hdr C (3), hdr E (5)
                max 5 hdr B (2)
                max 9 hdr C (3), hdr D (4)





                share|improve this answer




























                  0












                  0








                  0







                  The following allows for a repeating maximum on the same line.



                  awk -F, 'NR==1 { split($0,head,FS); next }
                  { max=0; delete a;
                  for(i=1;i<=NF;i++) if($i>=max){ max=$i; a[max]=a[max]head[i]" ("i"), " }
                  print "max " max "t" substr(a[max], 0, length(a[max])-2)
                  }' file


                  input:



                  hdr A,hdr B,hdr C,hdr D,hdr E,hdr F
                  5,2,7,4,7,-9
                  1,5,4,3,2,1
                  1,5,9,9,5,3


                  output:



                  max 7   hdr C (3), hdr E (5)
                  max 5 hdr B (2)
                  max 9 hdr C (3), hdr D (4)





                  share|improve this answer















                  The following allows for a repeating maximum on the same line.



                  awk -F, 'NR==1 { split($0,head,FS); next }
                  { max=0; delete a;
                  for(i=1;i<=NF;i++) if($i>=max){ max=$i; a[max]=a[max]head[i]" ("i"), " }
                  print "max " max "t" substr(a[max], 0, length(a[max])-2)
                  }' file


                  input:



                  hdr A,hdr B,hdr C,hdr D,hdr E,hdr F
                  5,2,7,4,7,-9
                  1,5,4,3,2,1
                  1,5,9,9,5,3


                  output:



                  max 7   hdr C (3), hdr E (5)
                  max 5 hdr B (2)
                  max 9 hdr C (3), hdr D (4)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jul 23 '15 at 17:18

























                  answered Jul 23 '15 at 9:36









                  Peter.OPeter.O

                  19.3k1892148




                  19.3k1892148























                      -1














                      The problem with CSV is it doesn't parse nicely with normal shell tools. They simply don't do it nicely. It can be done in trivial cases, but really - a scripting language is the tool for the job.



                      I'd be thinking more perl personally:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings;
                      use Text::CSV;

                      my $csv = Text::CSV->new();

                      open ( my $input, "<", "your_file.csv" ) or die $!;
                      $csv->column_names( $csv->getline( $input ) );

                      while ( my $row = $csv->getline_hr( $input ) ) {
                      my ( $highest, @rest ) = sort { $row->{$b} <=> $row->{$a} } keys %$row;
                      print join( "t", $highest, $row->{$highest} ), "n";
                      }


                      Which if using as input:



                      first,second,third,fourth
                      1,3,4,5,
                      5,4,3,2,
                      1,1,4,1,


                      Will print:



                      fourth  5
                      first 5
                      third 4





                      share|improve this answer



















                      • 1





                        "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                        – Wildcard
                        Nov 22 '16 at 10:54


















                      -1














                      The problem with CSV is it doesn't parse nicely with normal shell tools. They simply don't do it nicely. It can be done in trivial cases, but really - a scripting language is the tool for the job.



                      I'd be thinking more perl personally:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings;
                      use Text::CSV;

                      my $csv = Text::CSV->new();

                      open ( my $input, "<", "your_file.csv" ) or die $!;
                      $csv->column_names( $csv->getline( $input ) );

                      while ( my $row = $csv->getline_hr( $input ) ) {
                      my ( $highest, @rest ) = sort { $row->{$b} <=> $row->{$a} } keys %$row;
                      print join( "t", $highest, $row->{$highest} ), "n";
                      }


                      Which if using as input:



                      first,second,third,fourth
                      1,3,4,5,
                      5,4,3,2,
                      1,1,4,1,


                      Will print:



                      fourth  5
                      first 5
                      third 4





                      share|improve this answer



















                      • 1





                        "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                        – Wildcard
                        Nov 22 '16 at 10:54
















                      -1












                      -1








                      -1







                      The problem with CSV is it doesn't parse nicely with normal shell tools. They simply don't do it nicely. It can be done in trivial cases, but really - a scripting language is the tool for the job.



                      I'd be thinking more perl personally:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings;
                      use Text::CSV;

                      my $csv = Text::CSV->new();

                      open ( my $input, "<", "your_file.csv" ) or die $!;
                      $csv->column_names( $csv->getline( $input ) );

                      while ( my $row = $csv->getline_hr( $input ) ) {
                      my ( $highest, @rest ) = sort { $row->{$b} <=> $row->{$a} } keys %$row;
                      print join( "t", $highest, $row->{$highest} ), "n";
                      }


                      Which if using as input:



                      first,second,third,fourth
                      1,3,4,5,
                      5,4,3,2,
                      1,1,4,1,


                      Will print:



                      fourth  5
                      first 5
                      third 4





                      share|improve this answer













                      The problem with CSV is it doesn't parse nicely with normal shell tools. They simply don't do it nicely. It can be done in trivial cases, but really - a scripting language is the tool for the job.



                      I'd be thinking more perl personally:



                      #!/usr/bin/env perl
                      use strict;
                      use warnings;
                      use Text::CSV;

                      my $csv = Text::CSV->new();

                      open ( my $input, "<", "your_file.csv" ) or die $!;
                      $csv->column_names( $csv->getline( $input ) );

                      while ( my $row = $csv->getline_hr( $input ) ) {
                      my ( $highest, @rest ) = sort { $row->{$b} <=> $row->{$a} } keys %$row;
                      print join( "t", $highest, $row->{$highest} ), "n";
                      }


                      Which if using as input:



                      first,second,third,fourth
                      1,3,4,5,
                      5,4,3,2,
                      1,1,4,1,


                      Will print:



                      fourth  5
                      first 5
                      third 4






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jul 23 '15 at 9:24









                      SobriqueSobrique

                      3,859619




                      3,859619








                      • 1





                        "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                        – Wildcard
                        Nov 22 '16 at 10:54
















                      • 1





                        "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                        – Wildcard
                        Nov 22 '16 at 10:54










                      1




                      1





                      "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                      – Wildcard
                      Nov 22 '16 at 10:54







                      "CSVs don't parse nicely" — this is completely false for basic CSV files. With the "CSV" files that MS Excel uses (with embedded newlines and commas within the double quotes), this is true, but that's not the same thing as a straightforward Comma Separated Value file. (This is why this site has different tags for csv and csv-simple.)

                      – Wildcard
                      Nov 22 '16 at 10:54




















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f217817%2fhow-to-extract-column-name-header-from-a-csv-file-which-contains-the-max-value%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                      Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                      Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...