Find the largest positive number in my given data?

Clash Royale CLAN TAG#URR8PPP
Can someone show me a way to find the largest positive number using awk,sed or some sort of unix shell program? I'm using Linux RHEL with KSH. The input may have 8 columns worth of data with N/A as one of the possible values.
SAMPLE INPUT
1252.2 1251.8 N/A N/A
-31.9 -33.2 N/A N/A
-1172.4 -1174.4 N/A N/A
-6.5 -6.4 N/A N/A
-.3 -.3 N/A N/A
1351.8 1351.8 N/A N/A
38.3 38.0 N/A N/A
-21.6 -21.9 N/A N/A
-4.7 -4.5 N/A N/A
-5.0 -2.9 N/A N/A
3.1 3.3 N/A N/A
-20.1 -20.3 N/A N/A
-199.1 -199.3 N/A N/A
346.5 346.7 N/A N/A
-.8 -.4 N/A N/A
14.8 14.7 N/A N/A
8.4 8.4 N/A N/A
-18.2 -18.2 N/A N/A
-43.7 -43.6 N/A N/A
Desired output
Largest number is 1351.8
shell-script awk sed numeric-data
add a comment |
Can someone show me a way to find the largest positive number using awk,sed or some sort of unix shell program? I'm using Linux RHEL with KSH. The input may have 8 columns worth of data with N/A as one of the possible values.
SAMPLE INPUT
1252.2 1251.8 N/A N/A
-31.9 -33.2 N/A N/A
-1172.4 -1174.4 N/A N/A
-6.5 -6.4 N/A N/A
-.3 -.3 N/A N/A
1351.8 1351.8 N/A N/A
38.3 38.0 N/A N/A
-21.6 -21.9 N/A N/A
-4.7 -4.5 N/A N/A
-5.0 -2.9 N/A N/A
3.1 3.3 N/A N/A
-20.1 -20.3 N/A N/A
-199.1 -199.3 N/A N/A
346.5 346.7 N/A N/A
-.8 -.4 N/A N/A
14.8 14.7 N/A N/A
8.4 8.4 N/A N/A
-18.2 -18.2 N/A N/A
-43.7 -43.6 N/A N/A
Desired output
Largest number is 1351.8
shell-script awk sed numeric-data
add a comment |
Can someone show me a way to find the largest positive number using awk,sed or some sort of unix shell program? I'm using Linux RHEL with KSH. The input may have 8 columns worth of data with N/A as one of the possible values.
SAMPLE INPUT
1252.2 1251.8 N/A N/A
-31.9 -33.2 N/A N/A
-1172.4 -1174.4 N/A N/A
-6.5 -6.4 N/A N/A
-.3 -.3 N/A N/A
1351.8 1351.8 N/A N/A
38.3 38.0 N/A N/A
-21.6 -21.9 N/A N/A
-4.7 -4.5 N/A N/A
-5.0 -2.9 N/A N/A
3.1 3.3 N/A N/A
-20.1 -20.3 N/A N/A
-199.1 -199.3 N/A N/A
346.5 346.7 N/A N/A
-.8 -.4 N/A N/A
14.8 14.7 N/A N/A
8.4 8.4 N/A N/A
-18.2 -18.2 N/A N/A
-43.7 -43.6 N/A N/A
Desired output
Largest number is 1351.8
shell-script awk sed numeric-data
Can someone show me a way to find the largest positive number using awk,sed or some sort of unix shell program? I'm using Linux RHEL with KSH. The input may have 8 columns worth of data with N/A as one of the possible values.
SAMPLE INPUT
1252.2 1251.8 N/A N/A
-31.9 -33.2 N/A N/A
-1172.4 -1174.4 N/A N/A
-6.5 -6.4 N/A N/A
-.3 -.3 N/A N/A
1351.8 1351.8 N/A N/A
38.3 38.0 N/A N/A
-21.6 -21.9 N/A N/A
-4.7 -4.5 N/A N/A
-5.0 -2.9 N/A N/A
3.1 3.3 N/A N/A
-20.1 -20.3 N/A N/A
-199.1 -199.3 N/A N/A
346.5 346.7 N/A N/A
-.8 -.4 N/A N/A
14.8 14.7 N/A N/A
8.4 8.4 N/A N/A
-18.2 -18.2 N/A N/A
-43.7 -43.6 N/A N/A
Desired output
Largest number is 1351.8
shell-script awk sed numeric-data
shell-script awk sed numeric-data
edited Feb 24 at 4:08
Jeff Schaller
43.8k1161141
43.8k1161141
asked Feb 24 at 2:31
ayrton_sennaayrton_senna
5142419
5142419
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
A bash-style solution using tr, sort and head
echo $(< file) | tr ' ' 'n' | sort -rn | head -1
echo $(< file)reads the file and echoes the result. But as we don't have quotes around it (echo "$(< file)"does the same ascat file) the content is one long string with newlines removed and whitespace squeezed| tr ' ' 'n'pipes the result throughtrreplacing each space character with a newline| sort -rnpipes the result tosort, sorting all lines in reversed (-r) numeric (-n) order (largest number first)| head -1pipes the result toheadto output the first line
replacing head -1 with tail -1 will give the smallest. Nice. This is exactly what i want.
– ayrton_senna
Feb 24 at 5:16
Yes. Or you can sort withsort -nto get the smallest :)
– Freddy
Feb 24 at 5:17
2
I don't see the point of theecho $(<file), starting the pipeline with a<file tr -s ' ' 'n'should suffice
– iruvar
Feb 24 at 5:18
@iruvar Correct.
– Freddy
Feb 24 at 5:20
How abouttr ' ' 'n' < file | sort -rn | head -1
– Charles Green
Feb 24 at 5:21
|
show 1 more comment
With Awk, forcing numeric evaluation of the N/A entries:
awk 'for (i=1;i<=NF;i++) max = $i+0 > max ? $i : max ENDprint "Largest number is " max' data
Why the$i+0in themaxassignment's test ? Am I missing something ?
– Cbhihe
Feb 24 at 12:00
1
@Cbhihe it was intended to prevent unintended lexical comparison of any non-numeric fields - but perhaps it's not necessary
– steeldriver
Feb 24 at 16:54
add a comment |
One way can be:
perl -lane '$_>$m and $m=$_ for @F}print $m' sample.txt
Explanation:
-noption will process your input file on a per line basis.-eoption will apply the Perl code following it, to every line of input read.-aoption will split each line as it is read into individual fields and store them in an array@F.-loption will make RS=ORS="n"for @Fwill loop over each field, the current field being held in$_.We compare the current field with the maximum and in case it it more than max, update max.
When we are done comparing,
One way can be:
perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt
Explanation:
-noption will process your input file on a per line basis.-eoption will apply the Perl code following it, to every line of input read.-aoption will split each line as it is read into individual fields and store them in an array@F.-loption will make RS=ORS="n"for @Fwill loop over each field, the current field being held in$_.We compare the current field with the maximum and in case it it more than max, update max.
When we are done comparing,
One way can be:
perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt
Explanation:
-noption will process your input file on a per line basis.-eoption will apply the Perl code following it, to every line of input read.-aoption will split each line as it is read into individual fields and store them in an array@F.-loption will make RS=ORS="n"for @Fwill loop over each field, the current field being held in$_.We compare the current field with the maximum and in case it it more than max, update max.
When we are done comparing,
One way can be:
perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt
Explanation:
-noption will process your input file on a per line basis.-eoption will apply the Perl code following it, to every line of input read.-aoption will split each line as it is read into individual fields and store them in an array@F.-loption will make RS=ORS="n"for @Fwill loop over each field, the current field being held in$_.We compare the current field with the maximum and in case it it more than max, update max.
When we are done comparing, we print what is held in max.
The
N/As shall be treated as 0 in numeric context so won't affect the result, unless of course all numerics were negative to begin with.
edited Feb 24 at 13:54
Cbhihe
4021618
4021618
answered Feb 24 at 2:39
Rakesh SharmaRakesh Sharma
342115
342115
this is working fine. Can you explain how it works ?
– ayrton_senna
Feb 24 at 4:47
Done. Please check.
– Rakesh Sharma
Feb 24 at 5:22
add a comment |
this is working fine. Can you explain how it works ?
– ayrton_senna
Feb 24 at 4:47
Done. Please check.
– Rakesh Sharma
Feb 24 at 5:22
this is working fine. Can you explain how it works ?
– ayrton_senna
Feb 24 at 4:47
this is working fine. Can you explain how it works ?
– ayrton_senna
Feb 24 at 4:47
Done. Please check.
– Rakesh Sharma
Feb 24 at 5:22
Done. Please check.
– Rakesh Sharma
Feb 24 at 5:22
add a comment |
I have done by below method
Command
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
Output
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
output
1351.8
add a comment |
I have done by below method
Command
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
Output
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
output
1351.8
add a comment |
I have done by below method
Command
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
Output
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
output
1351.8
I have done by below method
Command
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
Output
awk 'print $0' o.txt | sed -r "s/s+/n/g"| sed '/^$/d'| sort | uniq| sed -n '/[0-9]/p'| awk 'BEGINsum=0;m=" "($1 >sum)sum=$1m=sumENDprint m'
output
1351.8
answered Feb 24 at 15:32
Praveen Kumar BSPraveen Kumar BS
1,6351311
1,6351311
add a comment |
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%2f502616%2ffind-the-largest-positive-number-in-my-given-data%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