When I write the awk script it print one line multiple times

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











up vote
1
down vote

favorite












I have written this script it print same line multiple times. what to do if I want to print it only one time?



BEGIN print "Average of salary"
cnt=cnt+1
total=total+$3
avg=total/cnt
END printf "Number of records:%d avg is:%d",cnt,avg






share|improve this question

























    up vote
    1
    down vote

    favorite












    I have written this script it print same line multiple times. what to do if I want to print it only one time?



    BEGIN print "Average of salary"
    cnt=cnt+1
    total=total+$3
    avg=total/cnt
    END printf "Number of records:%d avg is:%d",cnt,avg






    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have written this script it print same line multiple times. what to do if I want to print it only one time?



      BEGIN print "Average of salary"
      cnt=cnt+1
      total=total+$3
      avg=total/cnt
      END printf "Number of records:%d avg is:%d",cnt,avg






      share|improve this question













      I have written this script it print same line multiple times. what to do if I want to print it only one time?



      BEGIN print "Average of salary"
      cnt=cnt+1
      total=total+$3
      avg=total/cnt
      END printf "Number of records:%d avg is:%d",cnt,avg








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 29 at 7:56









      αғsнιη

      14.8k82462




      14.8k82462









      asked Apr 29 at 7:34









      Shah Honey

      173




      173




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          The problem is that you didn't enclose the below code within braces as following and awk will meet those as True statement and print every line it read:




          cnt=cnt+1
          total=total+$3
          avg=total/cnt



          But instead you could do something like:



          awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile


          The NR value is incrementing on each record/line awk reads from the input file, means that when awk read all lines the value of NR is the line number of the last line (basically it points to the Record Number)






          share|improve this answer






























            up vote
            1
            down vote













            You almost make it.



            enclose compute line in like



            BEGIN ...
            compute goes here
            END printf "..."


            why triple print ?



            every assignation like foo=$3+foo is a positive test to awk, and default action is to print line.



            (and by the way, you only need to compute average in END clause)






            share|improve this answer





















              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',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440690%2fwhen-i-write-the-awk-script-it-print-one-line-multiple-times%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              The problem is that you didn't enclose the below code within braces as following and awk will meet those as True statement and print every line it read:




              cnt=cnt+1
              total=total+$3
              avg=total/cnt



              But instead you could do something like:



              awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile


              The NR value is incrementing on each record/line awk reads from the input file, means that when awk read all lines the value of NR is the line number of the last line (basically it points to the Record Number)






              share|improve this answer



























                up vote
                2
                down vote













                The problem is that you didn't enclose the below code within braces as following and awk will meet those as True statement and print every line it read:




                cnt=cnt+1
                total=total+$3
                avg=total/cnt



                But instead you could do something like:



                awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile


                The NR value is incrementing on each record/line awk reads from the input file, means that when awk read all lines the value of NR is the line number of the last line (basically it points to the Record Number)






                share|improve this answer

























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The problem is that you didn't enclose the below code within braces as following and awk will meet those as True statement and print every line it read:




                  cnt=cnt+1
                  total=total+$3
                  avg=total/cnt



                  But instead you could do something like:



                  awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile


                  The NR value is incrementing on each record/line awk reads from the input file, means that when awk read all lines the value of NR is the line number of the last line (basically it points to the Record Number)






                  share|improve this answer















                  The problem is that you didn't enclose the below code within braces as following and awk will meet those as True statement and print every line it read:




                  cnt=cnt+1
                  total=total+$3
                  avg=total/cnt



                  But instead you could do something like:



                  awk ' avg=(total+=$3)/NR END print "...", NR, avg ' <infile


                  The NR value is incrementing on each record/line awk reads from the input file, means that when awk read all lines the value of NR is the line number of the last line (basically it points to the Record Number)







                  share|improve this answer















                  share|improve this answer



                  share|improve this answer








                  edited Apr 29 at 8:07


























                  answered Apr 29 at 7:53









                  αғsнιη

                  14.8k82462




                  14.8k82462






















                      up vote
                      1
                      down vote













                      You almost make it.



                      enclose compute line in like



                      BEGIN ...
                      compute goes here
                      END printf "..."


                      why triple print ?



                      every assignation like foo=$3+foo is a positive test to awk, and default action is to print line.



                      (and by the way, you only need to compute average in END clause)






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        You almost make it.



                        enclose compute line in like



                        BEGIN ...
                        compute goes here
                        END printf "..."


                        why triple print ?



                        every assignation like foo=$3+foo is a positive test to awk, and default action is to print line.



                        (and by the way, you only need to compute average in END clause)






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          You almost make it.



                          enclose compute line in like



                          BEGIN ...
                          compute goes here
                          END printf "..."


                          why triple print ?



                          every assignation like foo=$3+foo is a positive test to awk, and default action is to print line.



                          (and by the way, you only need to compute average in END clause)






                          share|improve this answer













                          You almost make it.



                          enclose compute line in like



                          BEGIN ...
                          compute goes here
                          END printf "..."


                          why triple print ?



                          every assignation like foo=$3+foo is a positive test to awk, and default action is to print line.



                          (and by the way, you only need to compute average in END clause)







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Apr 29 at 7:53









                          Archemar

                          18.9k93365




                          18.9k93365






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f440690%2fwhen-i-write-the-awk-script-it-print-one-line-multiple-times%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?