Find the largest positive number in my given data?

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












0















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









share|improve this question




























    0















    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









    share|improve this question


























      0












      0








      0








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 24 at 4:08









      Jeff Schaller

      43.8k1161141




      43.8k1161141










      asked Feb 24 at 2:31









      ayrton_sennaayrton_senna

      5142419




      5142419




















          4 Answers
          4






          active

          oldest

          votes


















          1














          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 as cat file) the content is one long string with newlines removed and whitespace squeezed


          • | tr ' ' 'n' pipes the result through tr replacing each space character with a newline


          • | sort -rn pipes the result to sort, sorting all lines in reversed (-r) numeric (-n) order (largest number first)


          • | head -1 pipes the result to head to output the first line





          share|improve this answer

























          • 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 with sort -n to get the smallest :)

            – Freddy
            Feb 24 at 5:17






          • 2





            I don't see the point of the echo $(<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 about tr ' ' 'n' < file | sort -rn | head -1

            – Charles Green
            Feb 24 at 5:21


















          3














          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





          share|improve this answer























          • Why the $i+0 in the max assignment'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



















          2














          One way can be:



          perl -lane '$_>$m and $m=$_ for @F}print $m' sample.txt


          Explanation:



          1. -n option will process your input file on a per line basis.


          2. -e option will apply the Perl code following it, to every line of input read.


          3. -a option will split each line as it is read into individual fields and store them in an array @F.


          4. -l option will make RS=ORS="n"


          5. for @F will loop over each field, the current field being held in $_.


          6. We compare the current field with the maximum and in case it it more than max, update max.


          7. When we are done comparing,  










          2














          One way can be:



          perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt


          Explanation:



          1. -n option will process your input file on a per line basis.


          2. -e option will apply the Perl code following it, to every line of input read.


          3. -a option will split each line as it is read into individual fields and store them in an array @F.


          4. -l option will make RS=ORS="n"


          5. for @F will loop over each field, the current field being held in $_.


          6. We compare the current field with the maximum and in case it it more than max, update max.


          7. When we are done comparing,  











          2














          One way can be:



          perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt


          Explanation:



          1. -n option will process your input file on a per line basis.


          2. -e option will apply the Perl code following it, to every line of input read.


          3. -a option will split each line as it is read into individual fields and store them in an array @F.


          4. -l option will make RS=ORS="n"


          5. for @F will loop over each field, the current field being held in $_.


          6. We compare the current field with the maximum and in case it it more than max, update max.


          7. When we are done comparing,  









          2












          2








          2







          One way can be:



          perl -lane '$_>$m and $m=$_ for @Fprint $m' sample.txt


          Explanation:



          1. -n option will process your input file on a per line basis.


          2. -e option will apply the Perl code following it, to every line of input read.


          3. -a option will split each line as it is read into individual fields and store them in an array @F.


          4. -l option will make RS=ORS="n"


          5. for @F will loop over each field, the current field being held in $_.


          6. We compare the current field with the maximum and in case it it more than max, update max.


          7. When we are done comparing, we print what is held in max.


          8. 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.






          shareprint $m' sample.txt


          Explanation:



          1. -n option will process your input file on a per line basis.


          2. -e option will apply the Perl code following it, to every line of input read.


          3. -a option will split each line as it is read into individual fields and store them in an array @F.


          4. -l option will make RS=ORS="n"


          5. for @F will loop over each field, the current field being held in $_.


          6. We compare the current field with the maximum and in case it it more than max, update max.


          7. When we are done comparing, { we print what is held in max.


          8. 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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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

















          • 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











          0














          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





          share|improve this answer



























            0














            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





            share|improve this answer

























              0












              0








              0







              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





              share|improve this answer













              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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Feb 24 at 15:32









              Praveen Kumar BSPraveen Kumar BS

              1,6351311




              1,6351311



























                  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%2f502616%2ffind-the-largest-positive-number-in-my-given-data%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

                  Peggy Mitchell

                  Palaiologos

                  The Forum (Inglewood, California)