How to Replace a string by another in a text file

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











up vote
1
down vote

favorite












I would like to replace a given string in a text file into by another, but with one extra detail.



Example:



If the file content's is :



Library Hello1
===============
any text here
version: 0.1
--------------

Library Hello2
===============
any text here
version:0.1
--------------

Library Hello3
===============
any text here
version: 0.2
--------------


I would like to grep all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.



Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.



Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.







share|improve this question





















  • Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
    – Renardo
    Jun 21 at 10:08










  • Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
    – Jeff Schaller
    Jun 21 at 10:55














up vote
1
down vote

favorite












I would like to replace a given string in a text file into by another, but with one extra detail.



Example:



If the file content's is :



Library Hello1
===============
any text here
version: 0.1
--------------

Library Hello2
===============
any text here
version:0.1
--------------

Library Hello3
===============
any text here
version: 0.2
--------------


I would like to grep all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.



Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.



Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.







share|improve this question





















  • Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
    – Renardo
    Jun 21 at 10:08










  • Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
    – Jeff Schaller
    Jun 21 at 10:55












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I would like to replace a given string in a text file into by another, but with one extra detail.



Example:



If the file content's is :



Library Hello1
===============
any text here
version: 0.1
--------------

Library Hello2
===============
any text here
version:0.1
--------------

Library Hello3
===============
any text here
version: 0.2
--------------


I would like to grep all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.



Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.



Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.







share|improve this question













I would like to replace a given string in a text file into by another, but with one extra detail.



Example:



If the file content's is :



Library Hello1
===============
any text here
version: 0.1
--------------

Library Hello2
===============
any text here
version:0.1
--------------

Library Hello3
===============
any text here
version: 0.2
--------------


I would like to grep all lines containing the word Library and replace all "Library" by the other word, eg. "myStr". However this should only be done for those which version is, for example, 0.1. All the others should be ignored.



Please notice that the search/replace should be done inside each block of code. The blocks are delimited by ===== and -----.



Note: I can do search and replace, but I don't know how to simultaneously search for the version inside a block.









share|improve this question












share|improve this question




share|improve this question








edited Jun 21 at 13:27









Pedro Cardoso

32




32









asked Jun 21 at 9:44









Pedro

61




61











  • Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
    – Renardo
    Jun 21 at 10:08










  • Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
    – Jeff Schaller
    Jun 21 at 10:55
















  • Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
    – Renardo
    Jun 21 at 10:08










  • Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
    – Jeff Schaller
    Jun 21 at 10:55















Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
– Renardo
Jun 21 at 10:08




Is “any text here” always exactly one line? In this case I suggest that you use awk to read all parts of a section into three variables (for Library, any text, and version) and output the, possible modified, section after reading version or after reading --------------.
– Renardo
Jun 21 at 10:08












Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
– Jeff Schaller
Jun 21 at 10:55




Pedro, it appears you've suggested an edit with a new (separate) account; please register and use the first one so that you can edit your own posts.
– Jeff Schaller
Jun 21 at 10:55










1 Answer
1






active

oldest

votes

















up vote
0
down vote













You can try this sed



sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile


Look first for Library.

If find get all the following line until version.

When find look for 0.1 and make change if it's ok.






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%2f451057%2fhow-to-replace-a-string-by-another-in-a-text-file%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
    0
    down vote













    You can try this sed



    sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile


    Look first for Library.

    If find get all the following line until version.

    When find look for 0.1 and make change if it's ok.






    share|improve this answer

























      up vote
      0
      down vote













      You can try this sed



      sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile


      Look first for Library.

      If find get all the following line until version.

      When find look for 0.1 and make change if it's ok.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You can try this sed



        sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile


        Look first for Library.

        If find get all the following line until version.

        When find look for 0.1 and make change if it's ok.






        share|improve this answer













        You can try this sed



        sed '/^Library/!b;:A;N;/version/!bA;/0.1$/s/^Library/myStr/' infile


        Look first for Library.

        If find get all the following line until version.

        When find look for 0.1 and make change if it's ok.







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jun 21 at 11:29









        ctac_

        996116




        996116






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451057%2fhow-to-replace-a-string-by-another-in-a-text-file%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