Help with Awk and regex or any thing else

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











up vote
1
down vote

favorite












I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).



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 easy, I really need to learn regex.










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
    1
    down vote

    favorite












    I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).



    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 easy, I really need to learn regex.










    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
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).



      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 easy, I really need to learn regex.










      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 can't do regex to save my life, though I've been meaning to learn. As for awk, I know enough about it to know that it's what I should be using in this situation, but not how to actually use it (if that makes sense at all).



      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 easy, I really need to learn regex.







      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 19 mins ago









      ilkkachu

      52.7k679145




      52.7k679145






      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 31 mins ago









      Craig Smith

      61




      61




      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
          2
          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
            1
            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
              1
              down vote













              With GNU sed:



              $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < 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




















                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%2fhelp-with-awk-and-regex-or-any-thing-else%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
                2
                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
                  2
                  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
                    2
                    down vote










                    up vote
                    2
                    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 27 mins ago









                    steeldriver

                    32.6k34980




                    32.6k34980






















                        up vote
                        1
                        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
                          1
                          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
                            1
                            down vote










                            up vote
                            1
                            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 28 mins ago









                            Goro

                            7,20753168




                            7,20753168




















                                up vote
                                1
                                down vote













                                With GNU sed:



                                $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < 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
























                                  up vote
                                  1
                                  down vote













                                  With GNU sed:



                                  $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < 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






















                                    up vote
                                    1
                                    down vote










                                    up vote
                                    1
                                    down vote









                                    With GNU sed:



                                    $ sed -Ee 's/([0-9]+) usec, ([0-9]+) usec.*/delayMicroseconds(1);npulseIR(3500);/' < 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(3500);/' < 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










                                    answered 23 mins ago









                                    ilkkachu

                                    52.7k679145




                                    52.7k679145




















                                        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%2fhelp-with-awk-and-regex-or-any-thing-else%23new-answer', 'question_page');

                                        );

                                        Post as a guest













































































                                        Comments

                                        Popular posts from this blog

                                        What does second last employer means? [closed]

                                        Installing NextGIS Connect into QGIS 3?

                                        One-line joke