Print only lines with highest value [duplicate]

Clash 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?
text-processing awk scripting sort
marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas
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.
add a comment |Â
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?
text-processing awk scripting sort
marked as duplicate by fedorqui, Sundeep, Stéphane Chazelas
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
add a comment |Â
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?
text-processing awk scripting sort
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
text-processing awk scripting sort
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
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
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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 fieldmax 2- numeric operation to get the maximum value of the 2nd field per group
The output:
host1 222
host2 90
host3 8
add a comment |Â
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.
add a comment |Â
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
add a comment |Â
up vote
6
down vote
accepted
With GNU sort or compatible:
<input.txt sort -k2rn | sort -sbuk1,1 >output.txt
add a comment |Â
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
With GNU sort or compatible:
<input.txt sort -k2rn | sort -sbuk1,1 >output.txt
edited Mar 7 at 11:39
answered Mar 7 at 11:05
Stéphane Chazelas
280k53515847
280k53515847
add a comment |Â
add a comment |Â
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 fieldmax 2- numeric operation to get the maximum value of the 2nd field per group
The output:
host1 222
host2 90
host3 8
add a comment |Â
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 fieldmax 2- numeric operation to get the maximum value of the 2nd field per group
The output:
host1 222
host2 90
host3 8
add a comment |Â
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 fieldmax 2- numeric operation to get the maximum value of the 2nd field per group
The output:
host1 222
host2 90
host3 8
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 fieldmax 2- numeric operation to get the maximum value of the 2nd field per group
The output:
host1 222
host2 90
host3 8
edited Mar 7 at 11:25
answered Mar 7 at 11:02
RomanPerekhrest
22.4k12144
22.4k12144
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
answered Mar 7 at 11:18
ilkkachu
49.2k672136
49.2k672136
add a comment |Â
add a comment |Â
1
So what did you try? It is quite easy in awk :)
â fedorqui
Mar 7 at 10:59