Formatting output with Awk and regex

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











up vote
2
down vote

favorite












I have ~20 files all around 300 lines long, populated by data formatted like this:



62640 usec, 3500 usec
1640 usec, 480 usec
360 usec, 520 usec
1200 usec, 500 usec
340 usec, 520 usec


and I want to turn this into Arduino code, in the format



delayMicroseconds(62640);
pulseIR(3500);
delayMicroseconds(1640);
pulseIR(480);
delayMicroseconds(360);
pulseIR(520);
delayMicroseconds(1200);
pulseIR(500);
delayMicroseconds(340);
pulseIR(520);


and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.



Any ideas? I feel like this should be possible in awk.










share|improve this question









New contributor




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























    up vote
    2
    down vote

    favorite












    I have ~20 files all around 300 lines long, populated by data formatted like this:



    62640 usec, 3500 usec
    1640 usec, 480 usec
    360 usec, 520 usec
    1200 usec, 500 usec
    340 usec, 520 usec


    and I want to turn this into Arduino code, in the format



    delayMicroseconds(62640);
    pulseIR(3500);
    delayMicroseconds(1640);
    pulseIR(480);
    delayMicroseconds(360);
    pulseIR(520);
    delayMicroseconds(1200);
    pulseIR(500);
    delayMicroseconds(340);
    pulseIR(520);


    and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.



    Any ideas? I feel like this should be possible in awk.










    share|improve this question









    New contributor




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





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have ~20 files all around 300 lines long, populated by data formatted like this:



      62640 usec, 3500 usec
      1640 usec, 480 usec
      360 usec, 520 usec
      1200 usec, 500 usec
      340 usec, 520 usec


      and I want to turn this into Arduino code, in the format



      delayMicroseconds(62640);
      pulseIR(3500);
      delayMicroseconds(1640);
      pulseIR(480);
      delayMicroseconds(360);
      pulseIR(520);
      delayMicroseconds(1200);
      pulseIR(500);
      delayMicroseconds(340);
      pulseIR(520);


      and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.



      Any ideas? I feel like this should be possible in awk.










      share|improve this question









      New contributor




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











      I have ~20 files all around 300 lines long, populated by data formatted like this:



      62640 usec, 3500 usec
      1640 usec, 480 usec
      360 usec, 520 usec
      1200 usec, 500 usec
      340 usec, 520 usec


      and I want to turn this into Arduino code, in the format



      delayMicroseconds(62640);
      pulseIR(3500);
      delayMicroseconds(1640);
      pulseIR(480);
      delayMicroseconds(360);
      pulseIR(520);
      delayMicroseconds(1200);
      pulseIR(500);
      delayMicroseconds(340);
      pulseIR(520);


      and so on, where the number in the delayMicroseconds() function is the first number on each line, and the number in pulseIR() is the second number on each line.



      Any ideas? I feel like this should be possible in awk.







      text-processing awk






      share|improve this question









      New contributor




      Craig Smith 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




      Craig Smith 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 13 mins ago









      Rui F Ribeiro

      37k1273117




      37k1273117






      New contributor




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









      asked 10 hours ago









      Craig Smith

      111




      111




      New contributor




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





      New contributor





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






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




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          8
          down vote













          There's no need for any regex here - just printf to format the fields into a string:



          $ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
          delayMicroseconds(62640);
          pulseIR(3500);
          delayMicroseconds(1640);
          pulseIR(480);
          delayMicroseconds(360);
          pulseIR(520);
          delayMicroseconds(1200);
          pulseIR(500);
          delayMicroseconds(340);
          pulseIR(520);





          share|improve this answer



























            up vote
            6
            down vote













            you can try this:



             #!/bin/bash
            cat file |sed '1d' | while IFS= read line; do

            n1=$(echo $line | awk 'print $1')
            n2=$(echo $line | awk 'print $3')

            echo "
            delayMicroseconds($n1);
            pulseIR($n2);
            "
            done

            delayMicroseconds(62640);
            pulseIR(3500);
            delayMicroseconds(1640);
            pulseIR(480);
            delayMicroseconds(360);
            pulseIR(520);
            delayMicroseconds(1200);
            pulseIR(500);
            delayMicroseconds(340);
            pulseIR(520);


            Alternatively you may try perl as follows;



            perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
            delayMicroseconds(62640);
            pulseIR(3500);
            delayMicroseconds(1640);
            pulseIR(480);
            delayMicroseconds(360);
            pulseIR(520);
            delayMicroseconds(1200);
            pulseIR(500);
            delayMicroseconds(340);
            pulseIR(520);





            share|improve this answer



























              up vote
              3
              down vote













              With GNU sed:



              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(2);/' < data 
              delayMicroseconds(62640);
              pulseIR(3500);
              ...


              -E tells sed to use extended regular expressions(*), the s/pattern/replacement/ command runs a search-replace operation over the current line (sed repeats the instructions given for each input line).



              The pattern is ([0-9]+) usec, ([0-9]+) usec.* where [0-9] means any one digit, + any number of the previous "atom", and (...) saves ("captures") whatever was matched. So ([0-9]+) means any number of any digits, and saves the result. The letters are matched as-is, and the final .* matches any number of any characters, just to eat any possible garbage at end of line.



              In the replacement, 1 and 2 are replaced with the saved contents of the (...) groups (but the parenthesis themselves are literal here), and n means the newline characters (that may be not work on all seds).




              (*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s/// command.






              share|improve this answer






















              • Looks like you hardcoded 3500 instead of using 2...
                – Gerrit0
                6 hours ago










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



              );






              Craig Smith 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%2f473867%2fformatting-output-with-awk-and-regex%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              8
              down vote













              There's no need for any regex here - just printf to format the fields into a string:



              $ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
              delayMicroseconds(62640);
              pulseIR(3500);
              delayMicroseconds(1640);
              pulseIR(480);
              delayMicroseconds(360);
              pulseIR(520);
              delayMicroseconds(1200);
              pulseIR(500);
              delayMicroseconds(340);
              pulseIR(520);





              share|improve this answer
























                up vote
                8
                down vote













                There's no need for any regex here - just printf to format the fields into a string:



                $ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
                delayMicroseconds(62640);
                pulseIR(3500);
                delayMicroseconds(1640);
                pulseIR(480);
                delayMicroseconds(360);
                pulseIR(520);
                delayMicroseconds(1200);
                pulseIR(500);
                delayMicroseconds(340);
                pulseIR(520);





                share|improve this answer






















                  up vote
                  8
                  down vote










                  up vote
                  8
                  down vote









                  There's no need for any regex here - just printf to format the fields into a string:



                  $ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
                  delayMicroseconds(62640);
                  pulseIR(3500);
                  delayMicroseconds(1640);
                  pulseIR(480);
                  delayMicroseconds(360);
                  pulseIR(520);
                  delayMicroseconds(1200);
                  pulseIR(500);
                  delayMicroseconds(340);
                  pulseIR(520);





                  share|improve this answer












                  There's no need for any regex here - just printf to format the fields into a string:



                  $ awk 'printf("delayMicroseconds(%d);npulseIR(%d);n", $1, $3)' file
                  delayMicroseconds(62640);
                  pulseIR(3500);
                  delayMicroseconds(1640);
                  pulseIR(480);
                  delayMicroseconds(360);
                  pulseIR(520);
                  delayMicroseconds(1200);
                  pulseIR(500);
                  delayMicroseconds(340);
                  pulseIR(520);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 10 hours ago









                  steeldriver

                  32.7k34980




                  32.7k34980






















                      up vote
                      6
                      down vote













                      you can try this:



                       #!/bin/bash
                      cat file |sed '1d' | while IFS= read line; do

                      n1=$(echo $line | awk 'print $1')
                      n2=$(echo $line | awk 'print $3')

                      echo "
                      delayMicroseconds($n1);
                      pulseIR($n2);
                      "
                      done

                      delayMicroseconds(62640);
                      pulseIR(3500);
                      delayMicroseconds(1640);
                      pulseIR(480);
                      delayMicroseconds(360);
                      pulseIR(520);
                      delayMicroseconds(1200);
                      pulseIR(500);
                      delayMicroseconds(340);
                      pulseIR(520);


                      Alternatively you may try perl as follows;



                      perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
                      delayMicroseconds(62640);
                      pulseIR(3500);
                      delayMicroseconds(1640);
                      pulseIR(480);
                      delayMicroseconds(360);
                      pulseIR(520);
                      delayMicroseconds(1200);
                      pulseIR(500);
                      delayMicroseconds(340);
                      pulseIR(520);





                      share|improve this answer
























                        up vote
                        6
                        down vote













                        you can try this:



                         #!/bin/bash
                        cat file |sed '1d' | while IFS= read line; do

                        n1=$(echo $line | awk 'print $1')
                        n2=$(echo $line | awk 'print $3')

                        echo "
                        delayMicroseconds($n1);
                        pulseIR($n2);
                        "
                        done

                        delayMicroseconds(62640);
                        pulseIR(3500);
                        delayMicroseconds(1640);
                        pulseIR(480);
                        delayMicroseconds(360);
                        pulseIR(520);
                        delayMicroseconds(1200);
                        pulseIR(500);
                        delayMicroseconds(340);
                        pulseIR(520);


                        Alternatively you may try perl as follows;



                        perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
                        delayMicroseconds(62640);
                        pulseIR(3500);
                        delayMicroseconds(1640);
                        pulseIR(480);
                        delayMicroseconds(360);
                        pulseIR(520);
                        delayMicroseconds(1200);
                        pulseIR(500);
                        delayMicroseconds(340);
                        pulseIR(520);





                        share|improve this answer






















                          up vote
                          6
                          down vote










                          up vote
                          6
                          down vote









                          you can try this:



                           #!/bin/bash
                          cat file |sed '1d' | while IFS= read line; do

                          n1=$(echo $line | awk 'print $1')
                          n2=$(echo $line | awk 'print $3')

                          echo "
                          delayMicroseconds($n1);
                          pulseIR($n2);
                          "
                          done

                          delayMicroseconds(62640);
                          pulseIR(3500);
                          delayMicroseconds(1640);
                          pulseIR(480);
                          delayMicroseconds(360);
                          pulseIR(520);
                          delayMicroseconds(1200);
                          pulseIR(500);
                          delayMicroseconds(340);
                          pulseIR(520);


                          Alternatively you may try perl as follows;



                          perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
                          delayMicroseconds(62640);
                          pulseIR(3500);
                          delayMicroseconds(1640);
                          pulseIR(480);
                          delayMicroseconds(360);
                          pulseIR(520);
                          delayMicroseconds(1200);
                          pulseIR(500);
                          delayMicroseconds(340);
                          pulseIR(520);





                          share|improve this answer












                          you can try this:



                           #!/bin/bash
                          cat file |sed '1d' | while IFS= read line; do

                          n1=$(echo $line | awk 'print $1')
                          n2=$(echo $line | awk 'print $3')

                          echo "
                          delayMicroseconds($n1);
                          pulseIR($n2);
                          "
                          done

                          delayMicroseconds(62640);
                          pulseIR(3500);
                          delayMicroseconds(1640);
                          pulseIR(480);
                          delayMicroseconds(360);
                          pulseIR(520);
                          delayMicroseconds(1200);
                          pulseIR(500);
                          delayMicroseconds(340);
                          pulseIR(520);


                          Alternatively you may try perl as follows;



                          perl -pe 's/(d+)[D]+(d+)[D]+/delayMicroseconds($1);npulseIR($2);n/' file
                          delayMicroseconds(62640);
                          pulseIR(3500);
                          delayMicroseconds(1640);
                          pulseIR(480);
                          delayMicroseconds(360);
                          pulseIR(520);
                          delayMicroseconds(1200);
                          pulseIR(500);
                          delayMicroseconds(340);
                          pulseIR(520);






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 10 hours ago









                          Goro

                          7,43753169




                          7,43753169




















                              up vote
                              3
                              down vote













                              With GNU sed:



                              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(2);/' < data 
                              delayMicroseconds(62640);
                              pulseIR(3500);
                              ...


                              -E tells sed to use extended regular expressions(*), the s/pattern/replacement/ command runs a search-replace operation over the current line (sed repeats the instructions given for each input line).



                              The pattern is ([0-9]+) usec, ([0-9]+) usec.* where [0-9] means any one digit, + any number of the previous "atom", and (...) saves ("captures") whatever was matched. So ([0-9]+) means any number of any digits, and saves the result. The letters are matched as-is, and the final .* matches any number of any characters, just to eat any possible garbage at end of line.



                              In the replacement, 1 and 2 are replaced with the saved contents of the (...) groups (but the parenthesis themselves are literal here), and n means the newline characters (that may be not work on all seds).




                              (*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s/// command.






                              share|improve this answer






















                              • Looks like you hardcoded 3500 instead of using 2...
                                – Gerrit0
                                6 hours ago














                              up vote
                              3
                              down vote













                              With GNU sed:



                              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(2);/' < data 
                              delayMicroseconds(62640);
                              pulseIR(3500);
                              ...


                              -E tells sed to use extended regular expressions(*), the s/pattern/replacement/ command runs a search-replace operation over the current line (sed repeats the instructions given for each input line).



                              The pattern is ([0-9]+) usec, ([0-9]+) usec.* where [0-9] means any one digit, + any number of the previous "atom", and (...) saves ("captures") whatever was matched. So ([0-9]+) means any number of any digits, and saves the result. The letters are matched as-is, and the final .* matches any number of any characters, just to eat any possible garbage at end of line.



                              In the replacement, 1 and 2 are replaced with the saved contents of the (...) groups (but the parenthesis themselves are literal here), and n means the newline characters (that may be not work on all seds).




                              (*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s/// command.






                              share|improve this answer






















                              • Looks like you hardcoded 3500 instead of using 2...
                                – Gerrit0
                                6 hours ago












                              up vote
                              3
                              down vote










                              up vote
                              3
                              down vote









                              With GNU sed:



                              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(2);/' < data 
                              delayMicroseconds(62640);
                              pulseIR(3500);
                              ...


                              -E tells sed to use extended regular expressions(*), the s/pattern/replacement/ command runs a search-replace operation over the current line (sed repeats the instructions given for each input line).



                              The pattern is ([0-9]+) usec, ([0-9]+) usec.* where [0-9] means any one digit, + any number of the previous "atom", and (...) saves ("captures") whatever was matched. So ([0-9]+) means any number of any digits, and saves the result. The letters are matched as-is, and the final .* matches any number of any characters, just to eat any possible garbage at end of line.



                              In the replacement, 1 and 2 are replaced with the saved contents of the (...) groups (but the parenthesis themselves are literal here), and n means the newline characters (that may be not work on all seds).




                              (*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s/// command.






                              share|improve this answer














                              With GNU sed:



                              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(2);/' < data 
                              delayMicroseconds(62640);
                              pulseIR(3500);
                              ...


                              -E tells sed to use extended regular expressions(*), the s/pattern/replacement/ command runs a search-replace operation over the current line (sed repeats the instructions given for each input line).



                              The pattern is ([0-9]+) usec, ([0-9]+) usec.* where [0-9] means any one digit, + any number of the previous "atom", and (...) saves ("captures") whatever was matched. So ([0-9]+) means any number of any digits, and saves the result. The letters are matched as-is, and the final .* matches any number of any characters, just to eat any possible garbage at end of line.



                              In the replacement, 1 and 2 are replaced with the saved contents of the (...) groups (but the parenthesis themselves are literal here), and n means the newline characters (that may be not work on all seds).




                              (*)See Why does my regular expression work in X but not in Y? for the difference between the different regex types. Also, there are a number of tutorials for sed online. I suggest finding one or two and playing around with it, at least to acquaint yourself with the s/// command.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 3 hours ago

























                              answered 10 hours ago









                              ilkkachu

                              52.7k679145




                              52.7k679145











                              • Looks like you hardcoded 3500 instead of using 2...
                                – Gerrit0
                                6 hours ago
















                              • Looks like you hardcoded 3500 instead of using 2...
                                – Gerrit0
                                6 hours ago















                              Looks like you hardcoded 3500 instead of using 2...
                              – Gerrit0
                              6 hours ago




                              Looks like you hardcoded 3500 instead of using 2...
                              – Gerrit0
                              6 hours ago










                              Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.









                               

                              draft saved


                              draft discarded


















                              Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.












                              Craig Smith is a new contributor. Be nice, and check out our Code of Conduct.











                              Craig Smith 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%2f473867%2fformatting-output-with-awk-and-regex%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Comments

                              Popular posts from this blog

                              What does second last employer means? [closed]

                              List of Gilmore Girls characters

                              One-line joke