How to restrict awk math output to 2 decimal places












0















My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.



grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'


Output :



Free Cpu: 98.9588%

Used Cpu: 1.04125%


Desired output :



Free Cpu: 98.95%

Used Cpu: 1.04%









share|improve this question









New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    use printf: printf "Used Cpu: %.2f%%n", usage

    – glenn jackman
    12 hours ago






  • 1





    Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

    – glenn jackman
    12 hours ago
















0















My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.



grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'


Output :



Free Cpu: 98.9588%

Used Cpu: 1.04125%


Desired output :



Free Cpu: 98.95%

Used Cpu: 1.04%









share|improve this question









New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    use printf: printf "Used Cpu: %.2f%%n", usage

    – glenn jackman
    12 hours ago






  • 1





    Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

    – glenn jackman
    12 hours ago














0












0








0








My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.



grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'


Output :



Free Cpu: 98.9588%

Used Cpu: 1.04125%


Desired output :



Free Cpu: 98.95%

Used Cpu: 1.04%









share|improve this question









New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












My Command is below and its print Free CPU and Used CPU below format.
I want to restrict awk output to 2 decimal place.



grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} {free=($5)*100/($2+$3+$4+$5)} END {print "Used Cpu:",usage "%"} {print "Free Cpu:",free "%"}'


Output :



Free Cpu: 98.9588%

Used Cpu: 1.04125%


Desired output :



Free Cpu: 98.95%

Used Cpu: 1.04%






linux awk cpu






share|improve this question









New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 5 mins ago









Isaac

11.6k11752




11.6k11752






New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 12 hours ago









Sandeep SinghSandeep Singh

1




1




New contributor




Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Sandeep Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    use printf: printf "Used Cpu: %.2f%%n", usage

    – glenn jackman
    12 hours ago






  • 1





    Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

    – glenn jackman
    12 hours ago














  • 2





    use printf: printf "Used Cpu: %.2f%%n", usage

    – glenn jackman
    12 hours ago






  • 1





    Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

    – glenn jackman
    12 hours ago








2




2





use printf: printf "Used Cpu: %.2f%%n", usage

– glenn jackman
12 hours ago





use printf: printf "Used Cpu: %.2f%%n", usage

– glenn jackman
12 hours ago




1




1





Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

– glenn jackman
12 hours ago





Note, your END block only prints "Used Cpu" -- the block that prints "Free Cpu" is not in the END block, and wil therefore print for each line of input.

– glenn jackman
12 hours ago










2 Answers
2






active

oldest

votes


















3














A bit of code review:




  • awk can do what grep does, so you don't need the pipeline

  • add newlines to the awk code for reability


For example:



awk '$1 == "cpu" {
printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
}' /proc/stat


which outputs the nicely aligned



Used Cpu:  5.34%
Free Cpu: 94.63%





share|improve this answer

































    0














    An alternative way is to define an output format OFMT="%5.2f%%" to format all numeric output:



    $ awk -v OFMT="%5.2f%%" '
    $1 == "cpu" {
    print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
    print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
    }
    ' /proc/stat


    Used Cpu: 9.52%
    Free Cpu: 90.48%




    share























      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
      });


      }
      });






      Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496906%2fhow-to-restrict-awk-math-output-to-2-decimal-places%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      A bit of code review:




      • awk can do what grep does, so you don't need the pipeline

      • add newlines to the awk code for reability


      For example:



      awk '$1 == "cpu" {
      printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
      printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
      }' /proc/stat


      which outputs the nicely aligned



      Used Cpu:  5.34%
      Free Cpu: 94.63%





      share|improve this answer






























        3














        A bit of code review:




        • awk can do what grep does, so you don't need the pipeline

        • add newlines to the awk code for reability


        For example:



        awk '$1 == "cpu" {
        printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
        printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
        }' /proc/stat


        which outputs the nicely aligned



        Used Cpu:  5.34%
        Free Cpu: 94.63%





        share|improve this answer




























          3












          3








          3







          A bit of code review:




          • awk can do what grep does, so you don't need the pipeline

          • add newlines to the awk code for reability


          For example:



          awk '$1 == "cpu" {
          printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
          printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
          }' /proc/stat


          which outputs the nicely aligned



          Used Cpu:  5.34%
          Free Cpu: 94.63%





          share|improve this answer















          A bit of code review:




          • awk can do what grep does, so you don't need the pipeline

          • add newlines to the awk code for reability


          For example:



          awk '$1 == "cpu" {
          printf "Used Cpu: %5.2f%%n", 100*($2+$4)/($2+$4+$5)
          printf "Free Cpu: %5.2f%%n", 100*$5/($2+$3+$4+$5)
          }' /proc/stat


          which outputs the nicely aligned



          Used Cpu:  5.34%
          Free Cpu: 94.63%






          share|improve this answer














          share|improve this answer



          share|improve this answer








          answered 12 hours ago


























          community wiki





          glenn jackman


























              0














              An alternative way is to define an output format OFMT="%5.2f%%" to format all numeric output:



              $ awk -v OFMT="%5.2f%%" '
              $1 == "cpu" {
              print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
              print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
              }
              ' /proc/stat


              Used Cpu: 9.52%
              Free Cpu: 90.48%




              share




























                0














                An alternative way is to define an output format OFMT="%5.2f%%" to format all numeric output:



                $ awk -v OFMT="%5.2f%%" '
                $1 == "cpu" {
                print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
                print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
                }
                ' /proc/stat


                Used Cpu: 9.52%
                Free Cpu: 90.48%




                share


























                  0












                  0








                  0







                  An alternative way is to define an output format OFMT="%5.2f%%" to format all numeric output:



                  $ awk -v OFMT="%5.2f%%" '
                  $1 == "cpu" {
                  print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
                  print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
                  }
                  ' /proc/stat


                  Used Cpu: 9.52%
                  Free Cpu: 90.48%




                  share













                  An alternative way is to define an output format OFMT="%5.2f%%" to format all numeric output:



                  $ awk -v OFMT="%5.2f%%" '
                  $1 == "cpu" {
                  print( "Used Cpu: ", 100*($2+$4)/($2+$4+$5) )
                  print( "Free Cpu: ", 100*$5/($2+$3+$4+$5) )
                  }
                  ' /proc/stat


                  Used Cpu: 9.52%
                  Free Cpu: 90.48%





                  share











                  share


                  share










                  answered 9 mins ago









                  IsaacIsaac

                  11.6k11752




                  11.6k11752






















                      Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.













                      Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.












                      Sandeep Singh is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f496906%2fhow-to-restrict-awk-math-output-to-2-decimal-places%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

                      CARDNET

                      Boot-repair Failure: Unable to locate package grub-common:i386

                      濃尾地震