Replace last two occurences of commas with dots












0















In each line in I want to replace last two commas with dots



I found such two sed piped method but it does not work with sed on MacOS



$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo


Would there be a way which will work with any sed version? Not necessarily with sed only.










share|improve this question


















  • 2





    How does it fail on MacOS?

    – choroba
    Jan 11 at 9:23











  • Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

    – rowan
    Jan 11 at 9:29
















0















In each line in I want to replace last two commas with dots



I found such two sed piped method but it does not work with sed on MacOS



$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo


Would there be a way which will work with any sed version? Not necessarily with sed only.










share|improve this question


















  • 2





    How does it fail on MacOS?

    – choroba
    Jan 11 at 9:23











  • Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

    – rowan
    Jan 11 at 9:29














0












0








0








In each line in I want to replace last two commas with dots



I found such two sed piped method but it does not work with sed on MacOS



$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo


Would there be a way which will work with any sed version? Not necessarily with sed only.










share|improve this question














In each line in I want to replace last two commas with dots



I found such two sed piped method but it does not work with sed on MacOS



$ echo 'abc,def,12379,foo' | sed 's/(.*),/1./'|sed 's/(.*),/1./'
abc,def.12379.foo


Would there be a way which will work with any sed version? Not necessarily with sed only.







sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 11 at 9:18









ChrisChris

146112




146112








  • 2





    How does it fail on MacOS?

    – choroba
    Jan 11 at 9:23











  • Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

    – rowan
    Jan 11 at 9:29














  • 2





    How does it fail on MacOS?

    – choroba
    Jan 11 at 9:23











  • Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

    – rowan
    Jan 11 at 9:29








2




2





How does it fail on MacOS?

– choroba
Jan 11 at 9:23





How does it fail on MacOS?

– choroba
Jan 11 at 9:23













Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

– rowan
Jan 11 at 9:29





Can you add -e to the sed command? So like this: echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./'|sed -e 's/(.*),/1./'.

– rowan
Jan 11 at 9:29










3 Answers
3






active

oldest

votes


















1














Try this:



$ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
abc,def.12379.foo


This will leave lines with a single comma alone (eg foo,bar). That may or may not be what you want.



(...) capture groups and 1, 2 backrerefences in the replacement string should be supported in all versions of sed; I've tested the above on Unix v7.



PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?






share|improve this answer

































    0














    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'


    or



    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'





    share|improve this answer































      0














      Using awk, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,



      echo 'abc,def,12379,foo' |
      awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'





      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',
        autoActivateHeartbeat: false,
        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%2f493903%2freplace-last-two-occurences-of-commas-with-dots%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        Try this:



        $ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
        abc,def.12379.foo


        This will leave lines with a single comma alone (eg foo,bar). That may or may not be what you want.



        (...) capture groups and 1, 2 backrerefences in the replacement string should be supported in all versions of sed; I've tested the above on Unix v7.



        PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?






        share|improve this answer






























          1














          Try this:



          $ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
          abc,def.12379.foo


          This will leave lines with a single comma alone (eg foo,bar). That may or may not be what you want.



          (...) capture groups and 1, 2 backrerefences in the replacement string should be supported in all versions of sed; I've tested the above on Unix v7.



          PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?






          share|improve this answer




























            1












            1








            1







            Try this:



            $ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
            abc,def.12379.foo


            This will leave lines with a single comma alone (eg foo,bar). That may or may not be what you want.



            (...) capture groups and 1, 2 backrerefences in the replacement string should be supported in all versions of sed; I've tested the above on Unix v7.



            PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?






            share|improve this answer















            Try this:



            $ echo 'abc,def,12379,foo' | sed 's/,([^,]*),([^,]*)$/.1.2/'
            abc,def.12379.foo


            This will leave lines with a single comma alone (eg foo,bar). That may or may not be what you want.



            (...) capture groups and 1, 2 backrerefences in the replacement string should be supported in all versions of sed; I've tested the above on Unix v7.



            PS. Even the double sed from your question should work on MacOS; maybe your problem is that the lines are terminated by CarriageReturns instead of LineFeeds?







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 12 at 18:19

























            answered Jan 12 at 17:02









            mosvymosvy

            6,2711425




            6,2711425

























                0














                echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'


                or



                echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'





                share|improve this answer




























                  0














                  echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'


                  or



                  echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'





                  share|improve this answer


























                    0












                    0








                    0







                    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'


                    or



                    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'





                    share|improve this answer













                    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./' -e 's/(.*),/1./'


                    or



                    echo 'abc,def,12379,foo' | sed -e 's/(.*),/1./;s/(.*),/1./'






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 12 at 16:19









                    Emilio GalarragaEmilio Galarraga

                    50929




                    50929























                        0














                        Using awk, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,



                        echo 'abc,def,12379,foo' |
                        awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'





                        share|improve this answer






























                          0














                          Using awk, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,



                          echo 'abc,def,12379,foo' |
                          awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'





                          share|improve this answer




























                            0












                            0








                            0







                            Using awk, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,



                            echo 'abc,def,12379,foo' |
                            awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'





                            share|improve this answer















                            Using awk, treating the input as a set of comma-delimited fields, and joining the three last fields with dots,



                            echo 'abc,def,12379,foo' |
                            awk 'BEGIN { OFS=FS="," } { print $1, $2 "." $3 "." $4 }'






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 12 at 16:59

























                            answered Jan 12 at 16:47









                            KusalanandaKusalananda

                            124k16234385




                            124k16234385






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f493903%2freplace-last-two-occurences-of-commas-with-dots%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

                                How to make a Squid Proxy server?

                                Is this a new Fibonacci Identity?

                                19世紀