Find the min and max value of column 3 and print whole row
Clash Royale CLAN TAG#URR8PPP
I have huuuge sets of data consisting of 3 columns. I'd like to be able to find max and min values in the third column (there is mix of zeros and then negative values, so I want to find max negative and min negative value) and then print that particular row. How can I do that?
example input:
-6.4 -3.6 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
.
.
.
-2.4 -1.6 -14.50
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
.
.
.
-5.4 -2.6 -4.52
-5.4 -2.6 -4.53
-5.4 -2.6 -4.50
-5.4 -2.6 -0.00
-5.4 -2.6 -0.00
output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
text-processing numeric-data
add a comment |
I have huuuge sets of data consisting of 3 columns. I'd like to be able to find max and min values in the third column (there is mix of zeros and then negative values, so I want to find max negative and min negative value) and then print that particular row. How can I do that?
example input:
-6.4 -3.6 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
.
.
.
-2.4 -1.6 -14.50
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
.
.
.
-5.4 -2.6 -4.52
-5.4 -2.6 -4.53
-5.4 -2.6 -4.50
-5.4 -2.6 -0.00
-5.4 -2.6 -0.00
output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
text-processing numeric-data
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
1
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28
add a comment |
I have huuuge sets of data consisting of 3 columns. I'd like to be able to find max and min values in the third column (there is mix of zeros and then negative values, so I want to find max negative and min negative value) and then print that particular row. How can I do that?
example input:
-6.4 -3.6 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
.
.
.
-2.4 -1.6 -14.50
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
.
.
.
-5.4 -2.6 -4.52
-5.4 -2.6 -4.53
-5.4 -2.6 -4.50
-5.4 -2.6 -0.00
-5.4 -2.6 -0.00
output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
text-processing numeric-data
I have huuuge sets of data consisting of 3 columns. I'd like to be able to find max and min values in the third column (there is mix of zeros and then negative values, so I want to find max negative and min negative value) and then print that particular row. How can I do that?
example input:
-6.4 -3.6 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
-6.4 -3.5 -0.00
.
.
.
-2.4 -1.6 -14.50
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
-2.4 -1.5 -14.49
.
.
.
-5.4 -2.6 -4.52
-5.4 -2.6 -4.53
-5.4 -2.6 -4.50
-5.4 -2.6 -0.00
-5.4 -2.6 -0.00
output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
text-processing numeric-data
text-processing numeric-data
edited Jan 5 at 12:31
Jeff Schaller
39.5k1054126
39.5k1054126
asked Jan 5 at 3:43
LonelyStudentLonelyStudent
32
32
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
1
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28
add a comment |
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
1
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
1
1
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28
add a comment |
3 Answers
3
active
oldest
votes
You've already accepted an answer but here's an easier way:
egrep -v "0.00|^." file.txt | awk 'NR==1 print "min:",$0 ENDprint"max:",$0'
Output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
-As you don't want the zeroes even though they're actually the largest number, the egrep -v
command prints all lines except those starting with 0.00 and beginning with a "."
-After the above, the lines are already in order from smallest to largest. awk
then prints the first line with "min:" prepended and the last line with "max:" prepended.
If the values in that column happen to not be in order, you can pipe sort -k3
(assuming it's the third column you want to sort) after the egrep
command and then pipe it into the awk
command after it.
add a comment |
I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:
minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split()[2])
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)
print("min", minVal)
print("max", maxVal)
EDIT: given that you want the entire row, you should instead store minRow
and maxRow
. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a key
argument to specify a custom function.
add a comment |
You can try with awk :
awk -v 'f=1' '
NF==3
min = min < $3 ? min : $3
if ( min != oldmin )
a = $0
oldmin = min
if ( $3 < 0 )
if ( f )
max = min
f = 0
max = max > $3 ? max : $3
if ( max != oldmax )
oldmax = max
b = $0
END
print "min : " a RS "max : " b
' infile
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492619%2ffind-the-min-and-max-value-of-column-3-and-print-whole-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You've already accepted an answer but here's an easier way:
egrep -v "0.00|^." file.txt | awk 'NR==1 print "min:",$0 ENDprint"max:",$0'
Output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
-As you don't want the zeroes even though they're actually the largest number, the egrep -v
command prints all lines except those starting with 0.00 and beginning with a "."
-After the above, the lines are already in order from smallest to largest. awk
then prints the first line with "min:" prepended and the last line with "max:" prepended.
If the values in that column happen to not be in order, you can pipe sort -k3
(assuming it's the third column you want to sort) after the egrep
command and then pipe it into the awk
command after it.
add a comment |
You've already accepted an answer but here's an easier way:
egrep -v "0.00|^." file.txt | awk 'NR==1 print "min:",$0 ENDprint"max:",$0'
Output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
-As you don't want the zeroes even though they're actually the largest number, the egrep -v
command prints all lines except those starting with 0.00 and beginning with a "."
-After the above, the lines are already in order from smallest to largest. awk
then prints the first line with "min:" prepended and the last line with "max:" prepended.
If the values in that column happen to not be in order, you can pipe sort -k3
(assuming it's the third column you want to sort) after the egrep
command and then pipe it into the awk
command after it.
add a comment |
You've already accepted an answer but here's an easier way:
egrep -v "0.00|^." file.txt | awk 'NR==1 print "min:",$0 ENDprint"max:",$0'
Output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
-As you don't want the zeroes even though they're actually the largest number, the egrep -v
command prints all lines except those starting with 0.00 and beginning with a "."
-After the above, the lines are already in order from smallest to largest. awk
then prints the first line with "min:" prepended and the last line with "max:" prepended.
If the values in that column happen to not be in order, you can pipe sort -k3
(assuming it's the third column you want to sort) after the egrep
command and then pipe it into the awk
command after it.
You've already accepted an answer but here's an easier way:
egrep -v "0.00|^." file.txt | awk 'NR==1 print "min:",$0 ENDprint"max:",$0'
Output:
min: -2.4 -1.6 -14.50
max: -5.4 -2.6 -4.50
-As you don't want the zeroes even though they're actually the largest number, the egrep -v
command prints all lines except those starting with 0.00 and beginning with a "."
-After the above, the lines are already in order from smallest to largest. awk
then prints the first line with "min:" prepended and the last line with "max:" prepended.
If the values in that column happen to not be in order, you can pipe sort -k3
(assuming it's the third column you want to sort) after the egrep
command and then pipe it into the awk
command after it.
answered Jan 5 at 14:24
Nasir RileyNasir Riley
2,441249
2,441249
add a comment |
add a comment |
I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:
minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split()[2])
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)
print("min", minVal)
print("max", maxVal)
EDIT: given that you want the entire row, you should instead store minRow
and maxRow
. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a key
argument to specify a custom function.
add a comment |
I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:
minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split()[2])
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)
print("min", minVal)
print("max", maxVal)
EDIT: given that you want the entire row, you should instead store minRow
and maxRow
. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a key
argument to specify a custom function.
add a comment |
I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:
minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split()[2])
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)
print("min", minVal)
print("max", maxVal)
EDIT: given that you want the entire row, you should instead store minRow
and maxRow
. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a key
argument to specify a custom function.
I would suggest doing this in Python. If your data is whitespace-separated, it will be something as simple as:
minVal = None
maxVal = None
with open('file') as f:
for line in f:
# default: split on whitespace.
third = int(line.split()[2])
if not minVal:
# initialize the values
minVal = third
maxVal = third
else:
minVal = min(third, minVal)
maxVal = max(third, maxVal)
print("min", minVal)
print("max", maxVal)
EDIT: given that you want the entire row, you should instead store minRow
and maxRow
. It the past, when I have had to do something like this, I have used a custom min/max function. Both take a key
argument to specify a custom function.
answered Jan 5 at 4:02
baumbaum
1103
1103
add a comment |
add a comment |
You can try with awk :
awk -v 'f=1' '
NF==3
min = min < $3 ? min : $3
if ( min != oldmin )
a = $0
oldmin = min
if ( $3 < 0 )
if ( f )
max = min
f = 0
max = max > $3 ? max : $3
if ( max != oldmax )
oldmax = max
b = $0
END
print "min : " a RS "max : " b
' infile
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
add a comment |
You can try with awk :
awk -v 'f=1' '
NF==3
min = min < $3 ? min : $3
if ( min != oldmin )
a = $0
oldmin = min
if ( $3 < 0 )
if ( f )
max = min
f = 0
max = max > $3 ? max : $3
if ( max != oldmax )
oldmax = max
b = $0
END
print "min : " a RS "max : " b
' infile
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
add a comment |
You can try with awk :
awk -v 'f=1' '
NF==3
min = min < $3 ? min : $3
if ( min != oldmin )
a = $0
oldmin = min
if ( $3 < 0 )
if ( f )
max = min
f = 0
max = max > $3 ? max : $3
if ( max != oldmax )
oldmax = max
b = $0
END
print "min : " a RS "max : " b
' infile
You can try with awk :
awk -v 'f=1' '
NF==3
min = min < $3 ? min : $3
if ( min != oldmin )
a = $0
oldmin = min
if ( $3 < 0 )
if ( f )
max = min
f = 0
max = max > $3 ? max : $3
if ( max != oldmax )
oldmax = max
b = $0
END
print "min : " a RS "max : " b
' infile
answered Jan 5 at 8:12
ctac_ctac_
1,3891210
1,3891210
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
add a comment |
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
Thanks a lot!!!
– LonelyStudent
Jan 5 at 13:31
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492619%2ffind-the-min-and-max-value-of-column-3-and-print-whole-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Give an example of the data and the expected output.
– Nasir Riley
Jan 5 at 3:55
I've edited, hope it's clear now!
– LonelyStudent
Jan 5 at 4:02
1
Your expected output doesn't match your data. If the values in column three are all negative then the maximum value should be zero.
– Nasir Riley
Jan 5 at 4:10
Unfortunately there are negative zeros, but those data are meaningless. What I wrote is what I need to get.
– LonelyStudent
Jan 5 at 13:07
Zero is neither positive nor negative. I understand what you need but it's just stated wrongly.
– Nasir Riley
Jan 5 at 13:28