Print only lines with highest value [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite













This question already has an answer here:



  • Find the smallest numbers in the second column corresponding to index values in first column

    4 answers



I need to process a file like below in the bash script:



input.txt:



host1 53
host1 123
host2 0
host1 222
host3 1
host1 85
host1 25
host1 13
host3 8
host2 90


I need to get in the results only one line for each host based on maximum
value in column 2:



output.txt:



host1 222
host2 90
host3 8


Any ideas?







share|improve this question














marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 7 at 11:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    So what did you try? It is quite easy in awk :)
    – fedorqui
    Mar 7 at 10:59














up vote
0
down vote

favorite













This question already has an answer here:



  • Find the smallest numbers in the second column corresponding to index values in first column

    4 answers



I need to process a file like below in the bash script:



input.txt:



host1 53
host1 123
host2 0
host1 222
host3 1
host1 85
host1 25
host1 13
host3 8
host2 90


I need to get in the results only one line for each host based on maximum
value in column 2:



output.txt:



host1 222
host2 90
host3 8


Any ideas?







share|improve this question














marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 7 at 11:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1




    So what did you try? It is quite easy in awk :)
    – fedorqui
    Mar 7 at 10:59












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Find the smallest numbers in the second column corresponding to index values in first column

    4 answers



I need to process a file like below in the bash script:



input.txt:



host1 53
host1 123
host2 0
host1 222
host3 1
host1 85
host1 25
host1 13
host3 8
host2 90


I need to get in the results only one line for each host based on maximum
value in column 2:



output.txt:



host1 222
host2 90
host3 8


Any ideas?







share|improve this question















This question already has an answer here:



  • Find the smallest numbers in the second column corresponding to index values in first column

    4 answers



I need to process a file like below in the bash script:



input.txt:



host1 53
host1 123
host2 0
host1 222
host3 1
host1 85
host1 25
host1 13
host3 8
host2 90


I need to get in the results only one line for each host based on maximum
value in column 2:



output.txt:



host1 222
host2 90
host3 8


Any ideas?





This question already has an answer here:



  • Find the smallest numbers in the second column corresponding to index values in first column

    4 answers









share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 11:16









Jeff Schaller

31.2k846105




31.2k846105










asked Mar 7 at 10:56









Scyld de Fraud

1,841610




1,841610




marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 7 at 11:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas awk
Users with the  awk badge can single-handedly close awk questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 7 at 11:42


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 1




    So what did you try? It is quite easy in awk :)
    – fedorqui
    Mar 7 at 10:59












  • 1




    So what did you try? It is quite easy in awk :)
    – fedorqui
    Mar 7 at 10:59







1




1




So what did you try? It is quite easy in awk :)
– fedorqui
Mar 7 at 10:59




So what did you try? It is quite easy in awk :)
– fedorqui
Mar 7 at 10:59










3 Answers
3






active

oldest

votes

















up vote
6
down vote



accepted










With GNU sort or compatible:



<input.txt sort -k2rn | sort -sbuk1,1 >output.txt





share|improve this answer





























    up vote
    5
    down vote













    The shortest one with GNU datamash:



    datamash -sW -g1 max 2 <file



    • -s - sort the input before grouping


    • -W - use whitespace (one or more spaces and/or tabs) for field delimiters


    • -g 1 - group via 1st field


    • max 2 - numeric operation to get the maximum value of the 2nd field per group


    The output:



    host1 222
    host2 90
    host3 8





    share|improve this answer





























      up vote
      3
      down vote













      Here's the awk one-liner:



      $ awk ' END for (k in a) print k, a[k] ' < input.txt
      host1 222
      host2 90
      host3 8


      $1 and $2 are the first two fields of the line, associative array a[k] holds the current maximum for key k. With the test against a[$1] being empty, it should deal with negative numbers correctly.






      share|improve this answer



























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        6
        down vote



        accepted










        With GNU sort or compatible:



        <input.txt sort -k2rn | sort -sbuk1,1 >output.txt





        share|improve this answer


























          up vote
          6
          down vote



          accepted










          With GNU sort or compatible:



          <input.txt sort -k2rn | sort -sbuk1,1 >output.txt





          share|improve this answer
























            up vote
            6
            down vote



            accepted







            up vote
            6
            down vote



            accepted






            With GNU sort or compatible:



            <input.txt sort -k2rn | sort -sbuk1,1 >output.txt





            share|improve this answer














            With GNU sort or compatible:



            <input.txt sort -k2rn | sort -sbuk1,1 >output.txt






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 7 at 11:39

























            answered Mar 7 at 11:05









            Stéphane Chazelas

            280k53515847




            280k53515847






















                up vote
                5
                down vote













                The shortest one with GNU datamash:



                datamash -sW -g1 max 2 <file



                • -s - sort the input before grouping


                • -W - use whitespace (one or more spaces and/or tabs) for field delimiters


                • -g 1 - group via 1st field


                • max 2 - numeric operation to get the maximum value of the 2nd field per group


                The output:



                host1 222
                host2 90
                host3 8





                share|improve this answer


























                  up vote
                  5
                  down vote













                  The shortest one with GNU datamash:



                  datamash -sW -g1 max 2 <file



                  • -s - sort the input before grouping


                  • -W - use whitespace (one or more spaces and/or tabs) for field delimiters


                  • -g 1 - group via 1st field


                  • max 2 - numeric operation to get the maximum value of the 2nd field per group


                  The output:



                  host1 222
                  host2 90
                  host3 8





                  share|improve this answer
























                    up vote
                    5
                    down vote










                    up vote
                    5
                    down vote









                    The shortest one with GNU datamash:



                    datamash -sW -g1 max 2 <file



                    • -s - sort the input before grouping


                    • -W - use whitespace (one or more spaces and/or tabs) for field delimiters


                    • -g 1 - group via 1st field


                    • max 2 - numeric operation to get the maximum value of the 2nd field per group


                    The output:



                    host1 222
                    host2 90
                    host3 8





                    share|improve this answer














                    The shortest one with GNU datamash:



                    datamash -sW -g1 max 2 <file



                    • -s - sort the input before grouping


                    • -W - use whitespace (one or more spaces and/or tabs) for field delimiters


                    • -g 1 - group via 1st field


                    • max 2 - numeric operation to get the maximum value of the 2nd field per group


                    The output:



                    host1 222
                    host2 90
                    host3 8






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Mar 7 at 11:25

























                    answered Mar 7 at 11:02









                    RomanPerekhrest

                    22.4k12144




                    22.4k12144




















                        up vote
                        3
                        down vote













                        Here's the awk one-liner:



                        $ awk ' END for (k in a) print k, a[k] ' < input.txt
                        host1 222
                        host2 90
                        host3 8


                        $1 and $2 are the first two fields of the line, associative array a[k] holds the current maximum for key k. With the test against a[$1] being empty, it should deal with negative numbers correctly.






                        share|improve this answer
























                          up vote
                          3
                          down vote













                          Here's the awk one-liner:



                          $ awk ' END for (k in a) print k, a[k] ' < input.txt
                          host1 222
                          host2 90
                          host3 8


                          $1 and $2 are the first two fields of the line, associative array a[k] holds the current maximum for key k. With the test against a[$1] being empty, it should deal with negative numbers correctly.






                          share|improve this answer






















                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            Here's the awk one-liner:



                            $ awk ' END for (k in a) print k, a[k] ' < input.txt
                            host1 222
                            host2 90
                            host3 8


                            $1 and $2 are the first two fields of the line, associative array a[k] holds the current maximum for key k. With the test against a[$1] being empty, it should deal with negative numbers correctly.






                            share|improve this answer












                            Here's the awk one-liner:



                            $ awk ' END for (k in a) print k, a[k] ' < input.txt
                            host1 222
                            host2 90
                            host3 8


                            $1 and $2 are the first two fields of the line, associative array a[k] holds the current maximum for key k. With the test against a[$1] being empty, it should deal with negative numbers correctly.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 7 at 11:18









                            ilkkachu

                            49.2k672136




                            49.2k672136












                                Popular posts from this blog

                                Peggy Mitchell

                                Palaiologos

                                The Forum (Inglewood, California)