Using sed to replace “A” with a “B” or “C”How to generate a random string?Replace matches with multiline string using sedReorder Multiple Line Blocks with Sedreplace $ with £ using sed commandHow to replace symbol || with |“”| using sedReplace some text with sedReplace a range of text with special characters using sedReplace special characters with sedSearch and replace sentence with spaces using sed commandFind and replace using regex with sedUsing sed to replace numbers with @ inserts @ between every character

Can ADFS connect to other SSO services?

Sci fi short story, robot city that nags people about health

Require advice on power conservation for backpacking trip

Does squid ink pasta bleed?

Interaction between Leyline of Anticipation and Teferi, Time Raveler

Why doesn't a marching band have strings?

Should I prioritize my 401(k) over my student loans?

Cascading Repair Costs following Blown Head Gasket on a 2004 Subaru Outback

If the world have massive single giant world tree can it stop earthquake?

How do I set an alias to a terminal line?

“D’entre eux” to mean “of them”

Iterate MapThread with matrices

Iterate over colors on next pie chart with pgf-pie

Computing a trigonometric integral

Why cruise at 7000' in an A319?

In the Marvel universe, can a human have a baby with any non-human?

How does a blind passenger not die, if driver becomes unconscious

Why do all the teams that I have worked with always finish a sprint without completion of all the stories?

Where can I find a database of galactic spectra?

Can humans ever directly see a few photons at a time? Can a human see a single photon?

What is the origin of Scooby-Doo's name?

STM Microcontroller burns every time

What's currently blocking the construction of the wall between Mexico and the US?

Find the probability that the 8th woman to appear is in 17th position.



Using sed to replace “A” with a “B” or “C”


How to generate a random string?Replace matches with multiline string using sedReorder Multiple Line Blocks with Sedreplace $ with £ using sed commandHow to replace symbol || with |“”| using sedReplace some text with sedReplace a range of text with special characters using sedReplace special characters with sedSearch and replace sentence with spaces using sed commandFind and replace using regex with sedUsing sed to replace numbers with @ inserts @ between every character






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















This should be straight-forward, but I cannot figure it out. If I want to replace an A or B with a C using sed the code could potentially be:



$ echo AAXXAAYYBB | sed 's/[AB]/C/g'
CCXXCCYYCC


This results in all A's and B's converted to C's.



What I'd like to do is replace "A" with either one of two (or potentially more) variables:



Input:



AAXXAAYYBB


Code:



sed 's/A/[BC]/g'


Output (where the substitution of B or C is random):



BCXXCBYYBB 


But this code will only change A's to...



$ echo AAXXAAYYBB | sed 's/A/[BC]/g'
[BC][BC]XX[BC][BC]YYBB


I'm trying to avoid PERL here if possible. Does anyone have an idea how to fix this?










share|improve this question









New contributor



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



















  • The replacement text in the s command in sed is text, not an expression.

    – Kusalananda
    8 hours ago






  • 2





    It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

    – steeldriver
    7 hours ago











  • Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

    – Anthony D
    5 hours ago

















2















This should be straight-forward, but I cannot figure it out. If I want to replace an A or B with a C using sed the code could potentially be:



$ echo AAXXAAYYBB | sed 's/[AB]/C/g'
CCXXCCYYCC


This results in all A's and B's converted to C's.



What I'd like to do is replace "A" with either one of two (or potentially more) variables:



Input:



AAXXAAYYBB


Code:



sed 's/A/[BC]/g'


Output (where the substitution of B or C is random):



BCXXCBYYBB 


But this code will only change A's to...



$ echo AAXXAAYYBB | sed 's/A/[BC]/g'
[BC][BC]XX[BC][BC]YYBB


I'm trying to avoid PERL here if possible. Does anyone have an idea how to fix this?










share|improve this question









New contributor



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



















  • The replacement text in the s command in sed is text, not an expression.

    – Kusalananda
    8 hours ago






  • 2





    It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

    – steeldriver
    7 hours ago











  • Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

    – Anthony D
    5 hours ago













2












2








2








This should be straight-forward, but I cannot figure it out. If I want to replace an A or B with a C using sed the code could potentially be:



$ echo AAXXAAYYBB | sed 's/[AB]/C/g'
CCXXCCYYCC


This results in all A's and B's converted to C's.



What I'd like to do is replace "A" with either one of two (or potentially more) variables:



Input:



AAXXAAYYBB


Code:



sed 's/A/[BC]/g'


Output (where the substitution of B or C is random):



BCXXCBYYBB 


But this code will only change A's to...



$ echo AAXXAAYYBB | sed 's/A/[BC]/g'
[BC][BC]XX[BC][BC]YYBB


I'm trying to avoid PERL here if possible. Does anyone have an idea how to fix this?










share|improve this question









New contributor



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











This should be straight-forward, but I cannot figure it out. If I want to replace an A or B with a C using sed the code could potentially be:



$ echo AAXXAAYYBB | sed 's/[AB]/C/g'
CCXXCCYYCC


This results in all A's and B's converted to C's.



What I'd like to do is replace "A" with either one of two (or potentially more) variables:



Input:



AAXXAAYYBB


Code:



sed 's/A/[BC]/g'


Output (where the substitution of B or C is random):



BCXXCBYYBB 


But this code will only change A's to...



$ echo AAXXAAYYBB | sed 's/A/[BC]/g'
[BC][BC]XX[BC][BC]YYBB


I'm trying to avoid PERL here if possible. Does anyone have an idea how to fix this?







sed random






share|improve this question









New contributor



Anthony D 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



Anthony D 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 7 hours ago









Tomasz

10.8k7 gold badges35 silver badges73 bronze badges




10.8k7 gold badges35 silver badges73 bronze badges






New contributor



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








asked 8 hours ago









Anthony DAnthony D

111 bronze badge




111 bronze badge




New contributor



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




New contributor




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














  • The replacement text in the s command in sed is text, not an expression.

    – Kusalananda
    8 hours ago






  • 2





    It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

    – steeldriver
    7 hours ago











  • Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

    – Anthony D
    5 hours ago

















  • The replacement text in the s command in sed is text, not an expression.

    – Kusalananda
    8 hours ago






  • 2





    It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

    – steeldriver
    7 hours ago











  • Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

    – Anthony D
    5 hours ago
















The replacement text in the s command in sed is text, not an expression.

– Kusalananda
8 hours ago





The replacement text in the s command in sed is text, not an expression.

– Kusalananda
8 hours ago




2




2





It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

– steeldriver
7 hours ago





It would be fairly easy to make the replacements alternate between B and C (using conditional branching)

– steeldriver
7 hours ago













Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

– Anthony D
5 hours ago





Hello. Thank you for reviewing my question. I am going to try to see if I can make this work without using perl based on your suggestions. I am trying write a .sh that I hope anyone would be able to modify. If I have to add perl, it might be a little complicated for potential users.

– Anthony D
5 hours ago










4 Answers
4






active

oldest

votes


















2














Plenty of ways to skin this particular cat once we resort to scripting, but here is something I threw together - It may not be pretty, (and relies on a bash shell!) but it might help you out:



#!/bin/bash

TEXT="AAXXAAYYBB"

echo "Start: $TEXT"

# So long as there are un-converted 'A' in the input string...
while [[ "$TEXT" =~ A ]]
do
# .. convert one 'A' to a random choice of either 'B' or 'C'
TEXT=$(echo $TEXT | sed -e "s/A/$(((RANDOM%2>0))&&echo B || echo C)/")

# lets show how we are progressing...
echo "Progress: $TEXT"
done

# No more 'A' in the input string, we are done:
echo "End: $TEXT"


Example output:



First run:




Start: AAXXAAYYBB



Progress: BAXXAAYYBB



Progress: BBXXAAYYBB



Progress: BBXXBAYYBB



Progress: BBXXBCYYBB



End: BBXXBCYYBB




Second run:




Start: AAXXAAYYBB



Progress: CAXXAAYYBB



Progress: CBXXAAYYBB



Progress: CBXXCAYYBB



Progress: CBXXCBYYBB



End: CBXXCBYYBB







share|improve this answer








New contributor



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



















  • Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

    – Sparhawk
    6 hours ago


















1














It is possible to replace the first match of an string with:



$str/A/...


And, it is possible to generate a random (not a cryptographic secure number) value with:



r=(B C)
$r[RANDOM%2]


Each time the variable r is expanded.



So:



#!/bin/bash

str=AAXXAAYYBB
r=(B C)

while [[ $str =~ A ]]; do
str=$str/A/"$r[RANDOM%2]"
done

echo "str=$str"


Will generate a random result each time it is called.






share|improve this answer






























    0














    It is much more "straightforward" to replace A or B with C than to replace A with B or C, as the or in the former case has nothing to do with randomness, while in the latter it's based on it. So while the former could be broken down into two simple steps:



    1. replace A with C

    2. replace B with C

    In the latter case there's the whole lot of deciding which one should be the replacement in every particular case, B or C? What's the basis? How random should the randomness be?



    sed doesn't offer any random operations as far as I know. (While Perl should be a good tool.)



    See related:



    • How to generate a random string?


    • And other random tagged questions






    share|improve this answer






























      0














      Not Sed, but avoids Perl:



      $ echo AAXXAAYYBB | gawk '
      BEGINsrand()

      n = patsplit($0,a,/A/,s);
      for(i=1;i<=n;i++) printf("%s%s", rand() < 0.5 ? "B" : "C", s[i]);
      print ""

      '
      CBXXCCYYBB





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



        );






        Anthony D 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%2f526129%2fusing-sed-to-replace-a-with-a-b-or-c%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        2














        Plenty of ways to skin this particular cat once we resort to scripting, but here is something I threw together - It may not be pretty, (and relies on a bash shell!) but it might help you out:



        #!/bin/bash

        TEXT="AAXXAAYYBB"

        echo "Start: $TEXT"

        # So long as there are un-converted 'A' in the input string...
        while [[ "$TEXT" =~ A ]]
        do
        # .. convert one 'A' to a random choice of either 'B' or 'C'
        TEXT=$(echo $TEXT | sed -e "s/A/$(((RANDOM%2>0))&&echo B || echo C)/")

        # lets show how we are progressing...
        echo "Progress: $TEXT"
        done

        # No more 'A' in the input string, we are done:
        echo "End: $TEXT"


        Example output:



        First run:




        Start: AAXXAAYYBB



        Progress: BAXXAAYYBB



        Progress: BBXXAAYYBB



        Progress: BBXXBAYYBB



        Progress: BBXXBCYYBB



        End: BBXXBCYYBB




        Second run:




        Start: AAXXAAYYBB



        Progress: CAXXAAYYBB



        Progress: CBXXAAYYBB



        Progress: CBXXCAYYBB



        Progress: CBXXCBYYBB



        End: CBXXCBYYBB







        share|improve this answer








        New contributor



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



















        • Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

          – Sparhawk
          6 hours ago















        2














        Plenty of ways to skin this particular cat once we resort to scripting, but here is something I threw together - It may not be pretty, (and relies on a bash shell!) but it might help you out:



        #!/bin/bash

        TEXT="AAXXAAYYBB"

        echo "Start: $TEXT"

        # So long as there are un-converted 'A' in the input string...
        while [[ "$TEXT" =~ A ]]
        do
        # .. convert one 'A' to a random choice of either 'B' or 'C'
        TEXT=$(echo $TEXT | sed -e "s/A/$(((RANDOM%2>0))&&echo B || echo C)/")

        # lets show how we are progressing...
        echo "Progress: $TEXT"
        done

        # No more 'A' in the input string, we are done:
        echo "End: $TEXT"


        Example output:



        First run:




        Start: AAXXAAYYBB



        Progress: BAXXAAYYBB



        Progress: BBXXAAYYBB



        Progress: BBXXBAYYBB



        Progress: BBXXBCYYBB



        End: BBXXBCYYBB




        Second run:




        Start: AAXXAAYYBB



        Progress: CAXXAAYYBB



        Progress: CBXXAAYYBB



        Progress: CBXXCAYYBB



        Progress: CBXXCBYYBB



        End: CBXXCBYYBB







        share|improve this answer








        New contributor



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



















        • Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

          – Sparhawk
          6 hours ago













        2












        2








        2







        Plenty of ways to skin this particular cat once we resort to scripting, but here is something I threw together - It may not be pretty, (and relies on a bash shell!) but it might help you out:



        #!/bin/bash

        TEXT="AAXXAAYYBB"

        echo "Start: $TEXT"

        # So long as there are un-converted 'A' in the input string...
        while [[ "$TEXT" =~ A ]]
        do
        # .. convert one 'A' to a random choice of either 'B' or 'C'
        TEXT=$(echo $TEXT | sed -e "s/A/$(((RANDOM%2>0))&&echo B || echo C)/")

        # lets show how we are progressing...
        echo "Progress: $TEXT"
        done

        # No more 'A' in the input string, we are done:
        echo "End: $TEXT"


        Example output:



        First run:




        Start: AAXXAAYYBB



        Progress: BAXXAAYYBB



        Progress: BBXXAAYYBB



        Progress: BBXXBAYYBB



        Progress: BBXXBCYYBB



        End: BBXXBCYYBB




        Second run:




        Start: AAXXAAYYBB



        Progress: CAXXAAYYBB



        Progress: CBXXAAYYBB



        Progress: CBXXCAYYBB



        Progress: CBXXCBYYBB



        End: CBXXCBYYBB







        share|improve this answer








        New contributor



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









        Plenty of ways to skin this particular cat once we resort to scripting, but here is something I threw together - It may not be pretty, (and relies on a bash shell!) but it might help you out:



        #!/bin/bash

        TEXT="AAXXAAYYBB"

        echo "Start: $TEXT"

        # So long as there are un-converted 'A' in the input string...
        while [[ "$TEXT" =~ A ]]
        do
        # .. convert one 'A' to a random choice of either 'B' or 'C'
        TEXT=$(echo $TEXT | sed -e "s/A/$(((RANDOM%2>0))&&echo B || echo C)/")

        # lets show how we are progressing...
        echo "Progress: $TEXT"
        done

        # No more 'A' in the input string, we are done:
        echo "End: $TEXT"


        Example output:



        First run:




        Start: AAXXAAYYBB



        Progress: BAXXAAYYBB



        Progress: BBXXAAYYBB



        Progress: BBXXBAYYBB



        Progress: BBXXBCYYBB



        End: BBXXBCYYBB




        Second run:




        Start: AAXXAAYYBB



        Progress: CAXXAAYYBB



        Progress: CBXXAAYYBB



        Progress: CBXXCAYYBB



        Progress: CBXXCBYYBB



        End: CBXXCBYYBB








        share|improve this answer








        New contributor



        bunnymjh 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 answer



        share|improve this answer






        New contributor



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








        answered 7 hours ago









        bunnymjhbunnymjh

        211 bronze badge




        211 bronze badge




        New contributor



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




        New contributor




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














        • Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

          – Sparhawk
          6 hours ago

















        • Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

          – Sparhawk
          6 hours ago
















        Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

        – Sparhawk
        6 hours ago





        Nice idea (+1)! You should quote your variables though (echo "$TEXT") to preserve whitespace. Also, even better is printf to deal with escape characters. Even better is a herestring (<<< "$TEXT") to not open a new process.

        – Sparhawk
        6 hours ago













        1














        It is possible to replace the first match of an string with:



        $str/A/...


        And, it is possible to generate a random (not a cryptographic secure number) value with:



        r=(B C)
        $r[RANDOM%2]


        Each time the variable r is expanded.



        So:



        #!/bin/bash

        str=AAXXAAYYBB
        r=(B C)

        while [[ $str =~ A ]]; do
        str=$str/A/"$r[RANDOM%2]"
        done

        echo "str=$str"


        Will generate a random result each time it is called.






        share|improve this answer



























          1














          It is possible to replace the first match of an string with:



          $str/A/...


          And, it is possible to generate a random (not a cryptographic secure number) value with:



          r=(B C)
          $r[RANDOM%2]


          Each time the variable r is expanded.



          So:



          #!/bin/bash

          str=AAXXAAYYBB
          r=(B C)

          while [[ $str =~ A ]]; do
          str=$str/A/"$r[RANDOM%2]"
          done

          echo "str=$str"


          Will generate a random result each time it is called.






          share|improve this answer

























            1












            1








            1







            It is possible to replace the first match of an string with:



            $str/A/...


            And, it is possible to generate a random (not a cryptographic secure number) value with:



            r=(B C)
            $r[RANDOM%2]


            Each time the variable r is expanded.



            So:



            #!/bin/bash

            str=AAXXAAYYBB
            r=(B C)

            while [[ $str =~ A ]]; do
            str=$str/A/"$r[RANDOM%2]"
            done

            echo "str=$str"


            Will generate a random result each time it is called.






            share|improve this answer













            It is possible to replace the first match of an string with:



            $str/A/...


            And, it is possible to generate a random (not a cryptographic secure number) value with:



            r=(B C)
            $r[RANDOM%2]


            Each time the variable r is expanded.



            So:



            #!/bin/bash

            str=AAXXAAYYBB
            r=(B C)

            while [[ $str =~ A ]]; do
            str=$str/A/"$r[RANDOM%2]"
            done

            echo "str=$str"


            Will generate a random result each time it is called.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            IsaacIsaac

            13.4k1 gold badge21 silver badges59 bronze badges




            13.4k1 gold badge21 silver badges59 bronze badges





















                0














                It is much more "straightforward" to replace A or B with C than to replace A with B or C, as the or in the former case has nothing to do with randomness, while in the latter it's based on it. So while the former could be broken down into two simple steps:



                1. replace A with C

                2. replace B with C

                In the latter case there's the whole lot of deciding which one should be the replacement in every particular case, B or C? What's the basis? How random should the randomness be?



                sed doesn't offer any random operations as far as I know. (While Perl should be a good tool.)



                See related:



                • How to generate a random string?


                • And other random tagged questions






                share|improve this answer



























                  0














                  It is much more "straightforward" to replace A or B with C than to replace A with B or C, as the or in the former case has nothing to do with randomness, while in the latter it's based on it. So while the former could be broken down into two simple steps:



                  1. replace A with C

                  2. replace B with C

                  In the latter case there's the whole lot of deciding which one should be the replacement in every particular case, B or C? What's the basis? How random should the randomness be?



                  sed doesn't offer any random operations as far as I know. (While Perl should be a good tool.)



                  See related:



                  • How to generate a random string?


                  • And other random tagged questions






                  share|improve this answer

























                    0












                    0








                    0







                    It is much more "straightforward" to replace A or B with C than to replace A with B or C, as the or in the former case has nothing to do with randomness, while in the latter it's based on it. So while the former could be broken down into two simple steps:



                    1. replace A with C

                    2. replace B with C

                    In the latter case there's the whole lot of deciding which one should be the replacement in every particular case, B or C? What's the basis? How random should the randomness be?



                    sed doesn't offer any random operations as far as I know. (While Perl should be a good tool.)



                    See related:



                    • How to generate a random string?


                    • And other random tagged questions






                    share|improve this answer













                    It is much more "straightforward" to replace A or B with C than to replace A with B or C, as the or in the former case has nothing to do with randomness, while in the latter it's based on it. So while the former could be broken down into two simple steps:



                    1. replace A with C

                    2. replace B with C

                    In the latter case there's the whole lot of deciding which one should be the replacement in every particular case, B or C? What's the basis? How random should the randomness be?



                    sed doesn't offer any random operations as far as I know. (While Perl should be a good tool.)



                    See related:



                    • How to generate a random string?


                    • And other random tagged questions







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 7 hours ago









                    TomaszTomasz

                    10.8k7 gold badges35 silver badges73 bronze badges




                    10.8k7 gold badges35 silver badges73 bronze badges





















                        0














                        Not Sed, but avoids Perl:



                        $ echo AAXXAAYYBB | gawk '
                        BEGINsrand()

                        n = patsplit($0,a,/A/,s);
                        for(i=1;i<=n;i++) printf("%s%s", rand() < 0.5 ? "B" : "C", s[i]);
                        print ""

                        '
                        CBXXCCYYBB





                        share|improve this answer



























                          0














                          Not Sed, but avoids Perl:



                          $ echo AAXXAAYYBB | gawk '
                          BEGINsrand()

                          n = patsplit($0,a,/A/,s);
                          for(i=1;i<=n;i++) printf("%s%s", rand() < 0.5 ? "B" : "C", s[i]);
                          print ""

                          '
                          CBXXCCYYBB





                          share|improve this answer

























                            0












                            0








                            0







                            Not Sed, but avoids Perl:



                            $ echo AAXXAAYYBB | gawk '
                            BEGINsrand()

                            n = patsplit($0,a,/A/,s);
                            for(i=1;i<=n;i++) printf("%s%s", rand() < 0.5 ? "B" : "C", s[i]);
                            print ""

                            '
                            CBXXCCYYBB





                            share|improve this answer













                            Not Sed, but avoids Perl:



                            $ echo AAXXAAYYBB | gawk '
                            BEGINsrand()

                            n = patsplit($0,a,/A/,s);
                            for(i=1;i<=n;i++) printf("%s%s", rand() < 0.5 ? "B" : "C", s[i]);
                            print ""

                            '
                            CBXXCCYYBB






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 6 hours ago









                            steeldriversteeldriver

                            40k4 gold badges54 silver badges93 bronze badges




                            40k4 gold badges54 silver badges93 bronze badges




















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









                                draft saved

                                draft discarded


















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












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











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














                                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%2f526129%2fusing-sed-to-replace-a-with-a-b-or-c%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

                                Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                Ласкавець круглолистий Зміст Опис | Поширення | Галерея | Примітки | Посилання | Навігаційне меню58171138361-22960890446Bupleurum rotundifoliumEuro+Med PlantbasePlants of the World Online — Kew ScienceGermplasm Resources Information Network (GRIN)Ласкавецькн. VI : Літери Ком — Левиправивши або дописавши її