SED fails on MacOS in two cases : invalid repetition count(s) and bad flag in substitute command












2















I am trying to create a series of cross platform SED substitutions (Linux and MacOS) . I am getting two errors on MacOS for the two SED commands below which work fine under bash in Linux. What do I need to change to get them accepted under BSD style SED?



Note: In each command below, $sed_regex_sym is replaced with -r for Linux and -E for macOS.



sed -$sed_regex_sym "/.*[*]/!{/.*[|]{1}/s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]([_|\/])x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/3/g}"

sed -$sed_regex_sym "/^([[:space:]_|\/]{0,$N_HASH_CHARS})[A-Z][[:space:]]/s/([A-Z][[:space:]].{1,${N_STAT_CHARS_TO_KEEP}}).*/$(printf "%${N_STAT_INDENT_CHARS}s" " ")1/"


For two commands above, I get the following two errors respectively:



sed: 1: "/.*[*]/!{/.*[|]{1}/s/x ...": bad flag in substitute command: '}'

sed: 1: "/^([[:space:]_|\/]{0, ...": RE error: invalid repetition count(s)


For the latter substitution, I did try escaping the parenthesis for the match groups as I saw on a another post but that did not resolve the invalid repetition count error.



Update 12/01/2018: I determined the source of the issues above. Going in reverse order:



1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above



2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;

Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS










share|improve this question

























  • Side note: GNU sed has understood -E for quite a while.

    – Benjamin W.
    Nov 21 '18 at 20:20











  • Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

    – EmceeBC
    Nov 24 '18 at 0:44
















2















I am trying to create a series of cross platform SED substitutions (Linux and MacOS) . I am getting two errors on MacOS for the two SED commands below which work fine under bash in Linux. What do I need to change to get them accepted under BSD style SED?



Note: In each command below, $sed_regex_sym is replaced with -r for Linux and -E for macOS.



sed -$sed_regex_sym "/.*[*]/!{/.*[|]{1}/s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]([_|\/])x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/3/g}"

sed -$sed_regex_sym "/^([[:space:]_|\/]{0,$N_HASH_CHARS})[A-Z][[:space:]]/s/([A-Z][[:space:]].{1,${N_STAT_CHARS_TO_KEEP}}).*/$(printf "%${N_STAT_INDENT_CHARS}s" " ")1/"


For two commands above, I get the following two errors respectively:



sed: 1: "/.*[*]/!{/.*[|]{1}/s/x ...": bad flag in substitute command: '}'

sed: 1: "/^([[:space:]_|\/]{0, ...": RE error: invalid repetition count(s)


For the latter substitution, I did try escaping the parenthesis for the match groups as I saw on a another post but that did not resolve the invalid repetition count error.



Update 12/01/2018: I determined the source of the issues above. Going in reverse order:



1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above



2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;

Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS










share|improve this question

























  • Side note: GNU sed has understood -E for quite a while.

    – Benjamin W.
    Nov 21 '18 at 20:20











  • Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

    – EmceeBC
    Nov 24 '18 at 0:44














2












2








2








I am trying to create a series of cross platform SED substitutions (Linux and MacOS) . I am getting two errors on MacOS for the two SED commands below which work fine under bash in Linux. What do I need to change to get them accepted under BSD style SED?



Note: In each command below, $sed_regex_sym is replaced with -r for Linux and -E for macOS.



sed -$sed_regex_sym "/.*[*]/!{/.*[|]{1}/s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]([_|\/])x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/3/g}"

sed -$sed_regex_sym "/^([[:space:]_|\/]{0,$N_HASH_CHARS})[A-Z][[:space:]]/s/([A-Z][[:space:]].{1,${N_STAT_CHARS_TO_KEEP}}).*/$(printf "%${N_STAT_INDENT_CHARS}s" " ")1/"


For two commands above, I get the following two errors respectively:



sed: 1: "/.*[*]/!{/.*[|]{1}/s/x ...": bad flag in substitute command: '}'

sed: 1: "/^([[:space:]_|\/]{0, ...": RE error: invalid repetition count(s)


For the latter substitution, I did try escaping the parenthesis for the match groups as I saw on a another post but that did not resolve the invalid repetition count error.



Update 12/01/2018: I determined the source of the issues above. Going in reverse order:



1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above



2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;

Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS










share|improve this question
















I am trying to create a series of cross platform SED substitutions (Linux and MacOS) . I am getting two errors on MacOS for the two SED commands below which work fine under bash in Linux. What do I need to change to get them accepted under BSD style SED?



Note: In each command below, $sed_regex_sym is replaced with -r for Linux and -E for macOS.



sed -$sed_regex_sym "/.*[*]/!{/.*[|]{1}/s/x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]([_|\/])x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]/3/g}"

sed -$sed_regex_sym "/^([[:space:]_|\/]{0,$N_HASH_CHARS})[A-Z][[:space:]]/s/([A-Z][[:space:]].{1,${N_STAT_CHARS_TO_KEEP}}).*/$(printf "%${N_STAT_INDENT_CHARS}s" " ")1/"


For two commands above, I get the following two errors respectively:



sed: 1: "/.*[*]/!{/.*[|]{1}/s/x ...": bad flag in substitute command: '}'

sed: 1: "/^([[:space:]_|\/]{0, ...": RE error: invalid repetition count(s)


For the latter substitution, I did try escaping the parenthesis for the match groups as I saw on a another post but that did not resolve the invalid repetition count error.



Update 12/01/2018: I determined the source of the issues above. Going in reverse order:



1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above



2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;

Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS







regex bash macos sed bsd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 '18 at 21:47







EmceeBC

















asked Nov 21 '18 at 20:14









EmceeBCEmceeBC

114




114













  • Side note: GNU sed has understood -E for quite a while.

    – Benjamin W.
    Nov 21 '18 at 20:20











  • Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

    – EmceeBC
    Nov 24 '18 at 0:44



















  • Side note: GNU sed has understood -E for quite a while.

    – Benjamin W.
    Nov 21 '18 at 20:20











  • Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

    – EmceeBC
    Nov 24 '18 at 0:44

















Side note: GNU sed has understood -E for quite a while.

– Benjamin W.
Nov 21 '18 at 20:20





Side note: GNU sed has understood -E for quite a while.

– Benjamin W.
Nov 21 '18 at 20:20













Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

– EmceeBC
Nov 24 '18 at 0:44





Thanks for the note about SED, one of the Linux systems I am using it on is quite old but I will see if it supports -E

– EmceeBC
Nov 24 '18 at 0:44












1 Answer
1






active

oldest

votes


















0














Copied the answer from my update above.



I determined the source of the issues above. Going in reverse order:
1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above
2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;
Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53419829%2fsed-fails-on-macos-in-two-cases-invalid-repetition-counts-and-bad-flag-in-su%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Copied the answer from my update above.



    I determined the source of the issues above. Going in reverse order:
    1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above
    2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;
    Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS






    share|improve this answer




























      0














      Copied the answer from my update above.



      I determined the source of the issues above. Going in reverse order:
      1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above
      2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;
      Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS






      share|improve this answer


























        0












        0








        0







        Copied the answer from my update above.



        I determined the source of the issues above. Going in reverse order:
        1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above
        2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;
        Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS






        share|improve this answer













        Copied the answer from my update above.



        I determined the source of the issues above. Going in reverse order:
        1) The latter substitution was not actually a sed issue but instead, an earlier line in my script which executed wc ( word count ) to determine N_HASH_CHARS ended up with preceding spaces on macOS only. Trimming the spaces resolved the issues with the second sed command above
        2) The first sed statement above was in fact a compatibility problem between BSD sed and Linux sed. I found elsewhere in stackoverflow the embedded function lists in BSD using { } need to have the internal function list end with a ;
        Putting a ; just after the g for global substitution resolved the issue and works for both Linux and macOS







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 2 '18 at 0:33









        EmceeBCEmceeBC

        114




        114
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • 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%2fstackoverflow.com%2fquestions%2f53419829%2fsed-fails-on-macos-in-two-cases-invalid-repetition-counts-and-bad-flag-in-su%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)