replace name after specific syntax

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











up vote
-2
down vote

favorite












We have Bash script. We want to change the name - master02 with $machine_master in the Bash script.



 value=master02_up
#master02
http://master02.$domain:8080


How to change master02 to $machine_master, only if master02 is after http://master02.



Expected output:



 value=master02_up
#master02
http://$machine_master.$domain:8080






share|improve this question

















  • 1




    awk '/http/ sub(/master02/,"$machine_master")1' file
    – jasonwryan
    Jul 5 at 7:48






  • 1




    is $machine_master intended as variable or as literal string?
    – RomanPerekhrest
    Jul 5 at 7:59










  • @RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
    – Kusalananda
    Jul 5 at 9:54










  • @Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
    – RomanPerekhrest
    Jul 5 at 10:18















up vote
-2
down vote

favorite












We have Bash script. We want to change the name - master02 with $machine_master in the Bash script.



 value=master02_up
#master02
http://master02.$domain:8080


How to change master02 to $machine_master, only if master02 is after http://master02.



Expected output:



 value=master02_up
#master02
http://$machine_master.$domain:8080






share|improve this question

















  • 1




    awk '/http/ sub(/master02/,"$machine_master")1' file
    – jasonwryan
    Jul 5 at 7:48






  • 1




    is $machine_master intended as variable or as literal string?
    – RomanPerekhrest
    Jul 5 at 7:59










  • @RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
    – Kusalananda
    Jul 5 at 9:54










  • @Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
    – RomanPerekhrest
    Jul 5 at 10:18













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











We have Bash script. We want to change the name - master02 with $machine_master in the Bash script.



 value=master02_up
#master02
http://master02.$domain:8080


How to change master02 to $machine_master, only if master02 is after http://master02.



Expected output:



 value=master02_up
#master02
http://$machine_master.$domain:8080






share|improve this question













We have Bash script. We want to change the name - master02 with $machine_master in the Bash script.



 value=master02_up
#master02
http://master02.$domain:8080


How to change master02 to $machine_master, only if master02 is after http://master02.



Expected output:



 value=master02_up
#master02
http://$machine_master.$domain:8080








share|improve this question












share|improve this question




share|improve this question








edited Jul 5 at 9:50









slm♦

233k65479651




233k65479651









asked Jul 5 at 7:43









yael

1,809941




1,809941







  • 1




    awk '/http/ sub(/master02/,"$machine_master")1' file
    – jasonwryan
    Jul 5 at 7:48






  • 1




    is $machine_master intended as variable or as literal string?
    – RomanPerekhrest
    Jul 5 at 7:59










  • @RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
    – Kusalananda
    Jul 5 at 9:54










  • @Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
    – RomanPerekhrest
    Jul 5 at 10:18













  • 1




    awk '/http/ sub(/master02/,"$machine_master")1' file
    – jasonwryan
    Jul 5 at 7:48






  • 1




    is $machine_master intended as variable or as literal string?
    – RomanPerekhrest
    Jul 5 at 7:59










  • @RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
    – Kusalananda
    Jul 5 at 9:54










  • @Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
    – RomanPerekhrest
    Jul 5 at 10:18








1




1




awk '/http/ sub(/master02/,"$machine_master")1' file
– jasonwryan
Jul 5 at 7:48




awk '/http/ sub(/master02/,"$machine_master")1' file
– jasonwryan
Jul 5 at 7:48




1




1




is $machine_master intended as variable or as literal string?
– RomanPerekhrest
Jul 5 at 7:59




is $machine_master intended as variable or as literal string?
– RomanPerekhrest
Jul 5 at 7:59












@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
– Kusalananda
Jul 5 at 9:54




@RomanPerekhrest From the expected output, it looks as if it's intended to be a fixed string.
– Kusalananda
Jul 5 at 9:54












@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
– RomanPerekhrest
Jul 5 at 10:18





@Kusalananda, there are many unexpected cases in comprehension of user questions. I feel like it's better to obtain the real "picture" through elaboration (even if something seems simple) (based on experience of working with SO and U&L)
– RomanPerekhrest
Jul 5 at 10:18











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Using standard sed:



sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile


This replaces the exact string http://master02.$domain:8080 with http://$machine_master.$domain:8080 and writes the result to a new file.



The $ in $domain has to be escaped to not be interpreted as an "end of line" pattern. The $ in the replacement text does not need to be escaped as this part is not a pattern.



I'm using # as a delimiter for the sed substitute command (s) as the pattern and replacement text both contain / which is the default delimiter.



The command could also be shortened to



sed 's#http://master02#http://$machine_master#' file >newfile


if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).



Testing:



$ cat file
value=master02_up
#master02
http://master02.$domain:8080

$ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile

$ cat newfile
value=master02_up
#master02
http://$machine_master.$domain:8080





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



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453543%2freplace-name-after-specific-syntax%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Using standard sed:



    sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile


    This replaces the exact string http://master02.$domain:8080 with http://$machine_master.$domain:8080 and writes the result to a new file.



    The $ in $domain has to be escaped to not be interpreted as an "end of line" pattern. The $ in the replacement text does not need to be escaped as this part is not a pattern.



    I'm using # as a delimiter for the sed substitute command (s) as the pattern and replacement text both contain / which is the default delimiter.



    The command could also be shortened to



    sed 's#http://master02#http://$machine_master#' file >newfile


    if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).



    Testing:



    $ cat file
    value=master02_up
    #master02
    http://master02.$domain:8080

    $ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile

    $ cat newfile
    value=master02_up
    #master02
    http://$machine_master.$domain:8080





    share|improve this answer



























      up vote
      1
      down vote



      accepted










      Using standard sed:



      sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile


      This replaces the exact string http://master02.$domain:8080 with http://$machine_master.$domain:8080 and writes the result to a new file.



      The $ in $domain has to be escaped to not be interpreted as an "end of line" pattern. The $ in the replacement text does not need to be escaped as this part is not a pattern.



      I'm using # as a delimiter for the sed substitute command (s) as the pattern and replacement text both contain / which is the default delimiter.



      The command could also be shortened to



      sed 's#http://master02#http://$machine_master#' file >newfile


      if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).



      Testing:



      $ cat file
      value=master02_up
      #master02
      http://master02.$domain:8080

      $ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile

      $ cat newfile
      value=master02_up
      #master02
      http://$machine_master.$domain:8080





      share|improve this answer

























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Using standard sed:



        sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile


        This replaces the exact string http://master02.$domain:8080 with http://$machine_master.$domain:8080 and writes the result to a new file.



        The $ in $domain has to be escaped to not be interpreted as an "end of line" pattern. The $ in the replacement text does not need to be escaped as this part is not a pattern.



        I'm using # as a delimiter for the sed substitute command (s) as the pattern and replacement text both contain / which is the default delimiter.



        The command could also be shortened to



        sed 's#http://master02#http://$machine_master#' file >newfile


        if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).



        Testing:



        $ cat file
        value=master02_up
        #master02
        http://master02.$domain:8080

        $ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile

        $ cat newfile
        value=master02_up
        #master02
        http://$machine_master.$domain:8080





        share|improve this answer















        Using standard sed:



        sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile


        This replaces the exact string http://master02.$domain:8080 with http://$machine_master.$domain:8080 and writes the result to a new file.



        The $ in $domain has to be escaped to not be interpreted as an "end of line" pattern. The $ in the replacement text does not need to be escaped as this part is not a pattern.



        I'm using # as a delimiter for the sed substitute command (s) as the pattern and replacement text both contain / which is the default delimiter.



        The command could also be shortened to



        sed 's#http://master02#http://$machine_master#' file >newfile


        if it's safe to do so (this depends on the contents of the file and what instances of text you'd like to replace).



        Testing:



        $ cat file
        value=master02_up
        #master02
        http://master02.$domain:8080

        $ sed 's#http://master02.$domain:8080#http://$machine_master.$domain:8080#' file >newfile

        $ cat newfile
        value=master02_up
        #master02
        http://$machine_master.$domain:8080






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jul 5 at 8:03


























        answered Jul 5 at 7:53









        Kusalananda

        101k13199312




        101k13199312






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f453543%2freplace-name-after-specific-syntax%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay