change only part of the substring using sed

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











up vote
3
down vote

favorite












I have a file that contains a numbers copied from somewhere. It looks something like this:



02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03


I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.



I tried using sed like:
cat file.txt | sed -r "s/ /, /g"




But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','




If I use cat file.txt | sed -r "s/[0123456789] /, /g", I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.



How do I do this?










share|improve this question









New contributor




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















  • 1




    @Hello scipsycho. Please see below. is this what you want?
    – Goro
    2 hours ago














up vote
3
down vote

favorite












I have a file that contains a numbers copied from somewhere. It looks something like this:



02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03


I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.



I tried using sed like:
cat file.txt | sed -r "s/ /, /g"




But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','




If I use cat file.txt | sed -r "s/[0123456789] /, /g", I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.



How do I do this?










share|improve this question









New contributor




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















  • 1




    @Hello scipsycho. Please see below. is this what you want?
    – Goro
    2 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a file that contains a numbers copied from somewhere. It looks something like this:



02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03


I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.



I tried using sed like:
cat file.txt | sed -r "s/ /, /g"




But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','




If I use cat file.txt | sed -r "s/[0123456789] /, /g", I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.



How do I do this?










share|improve this question









New contributor




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











I have a file that contains a numbers copied from somewhere. It looks something like this:



02 12 04 01 07 10 11 06 08 05 03 15 13 00 14 09,
14 11 02 12 04 07 13 01 05 00 15 10 03 09 08 06,
04 02 01 11 10 13 07 08 15 09 12 05 06 03 00 14,
11 08 12 07 01 14 02 13 06 15 00 09 10 04 05 03


I now have to add comma after every number (basically to make it a C++ array) but as you can see it can be very tedious to do, especially if you have many of them.



I tried using sed like:
cat file.txt | sed -r "s/ /, /g"




But if I use this I am going to replace every "space" with ',space' and I only want to replace spaces that come after a digit with ','




If I use cat file.txt | sed -r "s/[0123456789] /, /g", I won't be able to get the same number before replacement. Thus, I only want to change some part of the substring.



How do I do this?







sed






share|improve this question









New contributor




scipsycho 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




scipsycho 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 10 mins ago









GAD3R

23k164896




23k164896






New contributor




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









asked 2 hours ago









scipsycho

182




182




New contributor




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





New contributor





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






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







  • 1




    @Hello scipsycho. Please see below. is this what you want?
    – Goro
    2 hours ago












  • 1




    @Hello scipsycho. Please see below. is this what you want?
    – Goro
    2 hours ago







1




1




@Hello scipsycho. Please see below. is this what you want?
– Goro
2 hours ago




@Hello scipsycho. Please see below. is this what you want?
– Goro
2 hours ago










4 Answers
4






active

oldest

votes

















up vote
3
down vote



accepted










cat file.txt | sed -r 's/([0-9]+)/1,/g'

02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,


Explanation:



First capturing group ([0-9]+)

Match a single character (i.e. number) present in the table [0-9]+
+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.





share|improve this answer


















  • 1




    Thanks! it worked! What does this '1' mean?
    – scipsycho
    2 hours ago










  • Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
    – Goro
    2 hours ago











  • So, those brackets tell sed what to include in '1', right?
    – scipsycho
    2 hours ago






  • 1




    Hi @Goro, thanks a lot for this explanation.
    – scipsycho
    2 hours ago






  • 1




    The 1 is called a "back reference" in the sed documentation.
    – RobertL
    2 hours ago

















up vote
1
down vote













You can just replace a space followed by any number of spaces by a comma:



sed 's/ */,/g' file


(if the spaces at the start of some lines are just a copy paste error)






share|improve this answer



























    up vote
    1
    down vote













    How about



    sed 's/ +/, /g' file
    02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
    14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
    04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
    11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03





    share|improve this answer



























      up vote
      1
      down vote













      This perl command will add a comma in between a digit and a space



      perl -pe 's/(?<=d)(?=s)/,/g' file





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



        );






        scipsycho 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%2f471875%2fchange-only-part-of-the-substring-using-sed%23new-answer', 'question_page');

        );

        Post as a guest






























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote



        accepted










        cat file.txt | sed -r 's/([0-9]+)/1,/g'

        02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
        14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
        04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
        11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,


        Explanation:



        First capturing group ([0-9]+)

        Match a single character (i.e. number) present in the table [0-9]+
        + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
        0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

        In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
        1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.





        share|improve this answer


















        • 1




          Thanks! it worked! What does this '1' mean?
          – scipsycho
          2 hours ago










        • Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
          – Goro
          2 hours ago











        • So, those brackets tell sed what to include in '1', right?
          – scipsycho
          2 hours ago






        • 1




          Hi @Goro, thanks a lot for this explanation.
          – scipsycho
          2 hours ago






        • 1




          The 1 is called a "back reference" in the sed documentation.
          – RobertL
          2 hours ago














        up vote
        3
        down vote



        accepted










        cat file.txt | sed -r 's/([0-9]+)/1,/g'

        02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
        14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
        04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
        11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,


        Explanation:



        First capturing group ([0-9]+)

        Match a single character (i.e. number) present in the table [0-9]+
        + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
        0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

        In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
        1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.





        share|improve this answer


















        • 1




          Thanks! it worked! What does this '1' mean?
          – scipsycho
          2 hours ago










        • Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
          – Goro
          2 hours ago











        • So, those brackets tell sed what to include in '1', right?
          – scipsycho
          2 hours ago






        • 1




          Hi @Goro, thanks a lot for this explanation.
          – scipsycho
          2 hours ago






        • 1




          The 1 is called a "back reference" in the sed documentation.
          – RobertL
          2 hours ago












        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        cat file.txt | sed -r 's/([0-9]+)/1,/g'

        02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
        14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
        04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
        11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,


        Explanation:



        First capturing group ([0-9]+)

        Match a single character (i.e. number) present in the table [0-9]+
        + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
        0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

        In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
        1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.





        share|improve this answer














        cat file.txt | sed -r 's/([0-9]+)/1,/g'

        02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,,
        14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,,
        04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,,
        11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03,


        Explanation:



        First capturing group ([0-9]+)

        Match a single character (i.e. number) present in the table [0-9]+
        + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)
        0-9 a single character in the range between 0 (index 48) and 9 (index 57) (case sensitive)

        In other words, the [0-9]+ pattern matches an integer number (without decimals) even Inside longer strings, even words.
        1 is called a "back reference" or "special escapes" in the sed documentation. It refers to the corresponding matching sub-expressions in the regexp. In other words, in this example, it inserts the contents of each captured number in the table followed by comma.






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 hours ago

























        answered 2 hours ago









        Goro

        4,85552358




        4,85552358







        • 1




          Thanks! it worked! What does this '1' mean?
          – scipsycho
          2 hours ago










        • Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
          – Goro
          2 hours ago











        • So, those brackets tell sed what to include in '1', right?
          – scipsycho
          2 hours ago






        • 1




          Hi @Goro, thanks a lot for this explanation.
          – scipsycho
          2 hours ago






        • 1




          The 1 is called a "back reference" in the sed documentation.
          – RobertL
          2 hours ago












        • 1




          Thanks! it worked! What does this '1' mean?
          – scipsycho
          2 hours ago










        • Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
          – Goro
          2 hours ago











        • So, those brackets tell sed what to include in '1', right?
          – scipsycho
          2 hours ago






        • 1




          Hi @Goro, thanks a lot for this explanation.
          – scipsycho
          2 hours ago






        • 1




          The 1 is called a "back reference" in the sed documentation.
          – RobertL
          2 hours ago







        1




        1




        Thanks! it worked! What does this '1' mean?
        – scipsycho
        2 hours ago




        Thanks! it worked! What does this '1' mean?
        – scipsycho
        2 hours ago












        Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
        – Goro
        2 hours ago





        Hi @scipsycho. ` 1` refers to the number within each cell in the field will be followed by a comma. :-)
        – Goro
        2 hours ago













        So, those brackets tell sed what to include in '1', right?
        – scipsycho
        2 hours ago




        So, those brackets tell sed what to include in '1', right?
        – scipsycho
        2 hours ago




        1




        1




        Hi @Goro, thanks a lot for this explanation.
        – scipsycho
        2 hours ago




        Hi @Goro, thanks a lot for this explanation.
        – scipsycho
        2 hours ago




        1




        1




        The 1 is called a "back reference" in the sed documentation.
        – RobertL
        2 hours ago




        The 1 is called a "back reference" in the sed documentation.
        – RobertL
        2 hours ago












        up vote
        1
        down vote













        You can just replace a space followed by any number of spaces by a comma:



        sed 's/ */,/g' file


        (if the spaces at the start of some lines are just a copy paste error)






        share|improve this answer
























          up vote
          1
          down vote













          You can just replace a space followed by any number of spaces by a comma:



          sed 's/ */,/g' file


          (if the spaces at the start of some lines are just a copy paste error)






          share|improve this answer






















            up vote
            1
            down vote










            up vote
            1
            down vote









            You can just replace a space followed by any number of spaces by a comma:



            sed 's/ */,/g' file


            (if the spaces at the start of some lines are just a copy paste error)






            share|improve this answer












            You can just replace a space followed by any number of spaces by a comma:



            sed 's/ */,/g' file


            (if the spaces at the start of some lines are just a copy paste error)







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 hours ago









            choroba

            24.8k34168




            24.8k34168




















                up vote
                1
                down vote













                How about



                sed 's/ +/, /g' file
                02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
                14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
                04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
                11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03





                share|improve this answer
























                  up vote
                  1
                  down vote













                  How about



                  sed 's/ +/, /g' file
                  02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
                  14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
                  04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
                  11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    How about



                    sed 's/ +/, /g' file
                    02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
                    14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
                    04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
                    11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03





                    share|improve this answer












                    How about



                    sed 's/ +/, /g' file
                    02, 12, 04, 01, 07, 10, 11, 06, 08, 05, 03, 15, 13, 00, 14, 09,
                    14, 11, 02, 12, 04, 07, 13, 01, 05, 00, 15, 10, 03, 09, 08, 06,
                    04, 02, 01, 11, 10, 13, 07, 08, 15, 09, 12, 05, 06, 03, 00, 14,
                    11, 08, 12, 07, 01, 14, 02, 13, 06, 15, 00, 09, 10, 04, 05, 03






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 2 hours ago









                    RudiC

                    1,3999




                    1,3999




















                        up vote
                        1
                        down vote













                        This perl command will add a comma in between a digit and a space



                        perl -pe 's/(?<=d)(?=s)/,/g' file





                        share|improve this answer
























                          up vote
                          1
                          down vote













                          This perl command will add a comma in between a digit and a space



                          perl -pe 's/(?<=d)(?=s)/,/g' file





                          share|improve this answer






















                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            This perl command will add a comma in between a digit and a space



                            perl -pe 's/(?<=d)(?=s)/,/g' file





                            share|improve this answer












                            This perl command will add a comma in between a digit and a space



                            perl -pe 's/(?<=d)(?=s)/,/g' file






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 1 hour ago









                            glenn jackman

                            48.1k365105




                            48.1k365105




















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









                                 

                                draft saved


                                draft discarded


















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












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











                                scipsycho 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%2f471875%2fchange-only-part-of-the-substring-using-sed%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                What does second last employer means? [closed]

                                One-line joke