Improving my AWK skills











up vote
4
down vote

favorite
2












This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)



Could some one improve this script so I can see where I could improve?

Does some one want to show me some crazy bummed version of this?

Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?



#!/usr/bin/awk -f
BEGIN{
FS=",";
recordsCursor=1;
number=0;
#spacer="_";
#copies=3;
}
{
while(number++ <=copies){
while(recordsCursor <= NF){
if(recordsCursor==1){
printf($recordsCursor);
printf(spacer);
printf("%.3d,",number);
}else if(NF != recordsCursor){
printf("%s," , $recordsCursor);
}else{
printf("%s" , $recordsCursor);
}
recordsCursor++;
}
recordsCursor=1;
print "";
}
print "";
number=0;
}



Command: ./test.awk -v copies=3 -v spacer=_ input



Given input:



madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web


Correct output:



madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web

madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web

madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web









share|improve this question




























    up vote
    4
    down vote

    favorite
    2












    This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)



    Could some one improve this script so I can see where I could improve?

    Does some one want to show me some crazy bummed version of this?

    Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?



    #!/usr/bin/awk -f
    BEGIN{
    FS=",";
    recordsCursor=1;
    number=0;
    #spacer="_";
    #copies=3;
    }
    {
    while(number++ <=copies){
    while(recordsCursor <= NF){
    if(recordsCursor==1){
    printf($recordsCursor);
    printf(spacer);
    printf("%.3d,",number);
    }else if(NF != recordsCursor){
    printf("%s," , $recordsCursor);
    }else{
    printf("%s" , $recordsCursor);
    }
    recordsCursor++;
    }
    recordsCursor=1;
    print "";
    }
    print "";
    number=0;
    }



    Command: ./test.awk -v copies=3 -v spacer=_ input



    Given input:



    madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
    madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
    madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web


    Correct output:



    madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
    madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
    madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
    madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web

    madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
    madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
    madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
    madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web

    madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
    madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
    madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
    madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web









    share|improve this question


























      up vote
      4
      down vote

      favorite
      2









      up vote
      4
      down vote

      favorite
      2






      2





      This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)



      Could some one improve this script so I can see where I could improve?

      Does some one want to show me some crazy bummed version of this?

      Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?



      #!/usr/bin/awk -f
      BEGIN{
      FS=",";
      recordsCursor=1;
      number=0;
      #spacer="_";
      #copies=3;
      }
      {
      while(number++ <=copies){
      while(recordsCursor <= NF){
      if(recordsCursor==1){
      printf($recordsCursor);
      printf(spacer);
      printf("%.3d,",number);
      }else if(NF != recordsCursor){
      printf("%s," , $recordsCursor);
      }else{
      printf("%s" , $recordsCursor);
      }
      recordsCursor++;
      }
      recordsCursor=1;
      print "";
      }
      print "";
      number=0;
      }



      Command: ./test.awk -v copies=3 -v spacer=_ input



      Given input:



      madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web


      Correct output:



      madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web

      madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web

      madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web









      share|improve this question















      This program works as I intended but I feel like I used some clumsy methods to get the out put I desired(especially with my use of print commands and variable declarations)



      Could some one improve this script so I can see where I could improve?

      Does some one want to show me some crazy bummed version of this?

      Is setting variables in the BEGIN block which "runs every line !@#!#@!" how you are supposed to set a variable in AWK?



      #!/usr/bin/awk -f
      BEGIN{
      FS=",";
      recordsCursor=1;
      number=0;
      #spacer="_";
      #copies=3;
      }
      {
      while(number++ <=copies){
      while(recordsCursor <= NF){
      if(recordsCursor==1){
      printf($recordsCursor);
      printf(spacer);
      printf("%.3d,",number);
      }else if(NF != recordsCursor){
      printf("%s," , $recordsCursor);
      }else{
      printf("%s" , $recordsCursor);
      }
      recordsCursor++;
      }
      recordsCursor=1;
      print "";
      }
      print "";
      number=0;
      }



      Command: ./test.awk -v copies=3 -v spacer=_ input



      Given input:



      madison_Leaderboard_728x90,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Bullseye_300x250,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Button_155x155,155x155,Button ads for Madison,no,,,,,,,_blank,image,web


      Correct output:



      madison_Leaderboard_728x90_001,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_002,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_003,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web
      madison_Leaderboard_728x90_004,728x90,Leaderboard ads for Madison,no,,,,,,,_blank,image,web

      madison_Bullseye_300x250_001,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_002,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_003,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web
      madison_Bullseye_300x250_004,300x250,Bullseye ads for Madison,no,,,,,,Bullseye,_blank,image,web

      madison_Button_155x155_001,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_002,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_003,155x155,Button ads for Madison,no,,,,,,,_blank,image,web
      madison_Button_155x155_004,155x155,Button ads for Madison,no,,,,,,,_blank,image,web






      text-processing scripting awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 at 23:58









      Rui F Ribeiro

      38.3k1477127




      38.3k1477127










      asked Sep 3 '11 at 18:06









      Prospero

      1,78562545




      1,78562545






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          awk -v copies=3 -v spacer=_ '
          BEGIN {OFS=FS=","}
          {
          field1 = $1
          for (i=1; i <= copies+1; i++) {
          $1 = sprintf("%s%s%03d", field1, spacer, i)
          print
          }
          print ""
          }
          '


          This takes advantage of awk recalculating $0 if any of the fields change value.






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


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f20058%2fimproving-my-awk-skills%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            6
            down vote



            accepted










            awk -v copies=3 -v spacer=_ '
            BEGIN {OFS=FS=","}
            {
            field1 = $1
            for (i=1; i <= copies+1; i++) {
            $1 = sprintf("%s%s%03d", field1, spacer, i)
            print
            }
            print ""
            }
            '


            This takes advantage of awk recalculating $0 if any of the fields change value.






            share|improve this answer

























              up vote
              6
              down vote



              accepted










              awk -v copies=3 -v spacer=_ '
              BEGIN {OFS=FS=","}
              {
              field1 = $1
              for (i=1; i <= copies+1; i++) {
              $1 = sprintf("%s%s%03d", field1, spacer, i)
              print
              }
              print ""
              }
              '


              This takes advantage of awk recalculating $0 if any of the fields change value.






              share|improve this answer























                up vote
                6
                down vote



                accepted







                up vote
                6
                down vote



                accepted






                awk -v copies=3 -v spacer=_ '
                BEGIN {OFS=FS=","}
                {
                field1 = $1
                for (i=1; i <= copies+1; i++) {
                $1 = sprintf("%s%s%03d", field1, spacer, i)
                print
                }
                print ""
                }
                '


                This takes advantage of awk recalculating $0 if any of the fields change value.






                share|improve this answer












                awk -v copies=3 -v spacer=_ '
                BEGIN {OFS=FS=","}
                {
                field1 = $1
                for (i=1; i <= copies+1; i++) {
                $1 = sprintf("%s%s%03d", field1, spacer, i)
                print
                }
                print ""
                }
                '


                This takes advantage of awk recalculating $0 if any of the fields change value.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 4 '11 at 2:09









                glenn jackman

                49.6k569106




                49.6k569106






























                    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.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2f20058%2fimproving-my-awk-skills%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

                    Morgemoulin

                    Scott Moir

                    Souastre