Search and replace a substring only if another substring is not presentSearch and replace with sedHow can I replace a string in a file(s)?Find & replace image URLs in one file from another fileHow to replace string of the file, which is present in tar file through UNIXsed append a text with many lines after matching of multiple strings while the text remains many lines in sed commandBash while loop search and replace using sedExtracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with <br> tag using sed?How to run different python scripts from command line by passing the script name as argumentchange only part of the substring using sedSed to replace lowercase and capital strings

How can I find an old paper when the usual methods fail?

What is the hottest thing in the universe?

Telephone number in spoken words

Why does this Jet Provost strikemaster have a textured leading edge?

How do I call a 6-digit Australian phone number with a US-based mobile phone?

Why did IBM make the PC BIOS source code public?

How to programatically get all linked items for a given Sitecore item?

Setting up a Mathematical Institute of Refereeing?

How to gracefully leave a company you helped start?

What evidence points to a long ō in the first syllable of nōscō's present-tense form?

Source that you can't tell your wife not to lend to others

Bringing Power Supplies on Plane?

How do figure out how powerful I am, when my abilities far exceed my knowledge?

What is a "soap"?

Is there a word for returning to unpreparedness?

What's the point of writing that I know will never be used or read?

Output the list of musical notes

The more + the + comparative degree

Will some rockets really collapse under their own weight?

Locked room poison mystery!

Why do my bicycle brakes get worse and feel more 'squishy" over time?

How can I shoot a bow using Strength instead of Dexterity?

Perpendicular symbol with litle square

Unconventional examples of mathematical modelling



Search and replace a substring only if another substring is not present


Search and replace with sedHow can I replace a string in a file(s)?Find & replace image URLs in one file from another fileHow to replace string of the file, which is present in tar file through UNIXsed append a text with many lines after matching of multiple strings while the text remains many lines in sed commandBash while loop search and replace using sedExtracting values from a text file having | pipe as a delimiter in text file using awk command and Replacing new lines with <br> tag using sed?How to run different python scripts from command line by passing the script name as argumentchange only part of the substring using sedSed to replace lowercase and capital strings






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








2















I have the following strings in a very large document:



1.test.html#
2.test.md#
3.http://test.html#
4.https://test.md#
5.http://test.md#
6.test2.md#


Now I want to replace every .md# with .html# but ONLY if there is no http in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?










share|improve this question






























    2















    I have the following strings in a very large document:



    1.test.html#
    2.test.md#
    3.http://test.html#
    4.https://test.md#
    5.http://test.md#
    6.test2.md#


    Now I want to replace every .md# with .html# but ONLY if there is no http in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?










    share|improve this question


























      2












      2








      2








      I have the following strings in a very large document:



      1.test.html#
      2.test.md#
      3.http://test.html#
      4.https://test.md#
      5.http://test.md#
      6.test2.md#


      Now I want to replace every .md# with .html# but ONLY if there is no http in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?










      share|improve this question














      I have the following strings in a very large document:



      1.test.html#
      2.test.md#
      3.http://test.html#
      4.https://test.md#
      5.http://test.md#
      6.test2.md#


      Now I want to replace every .md# with .html# but ONLY if there is no http in the string. So only 2 and 6 should have a replacement. How can I do this in a shell script?







      shell-script sed






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 4 at 5:26









      JeroenJeroen

      1134 bronze badges




      1134 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          7














          With GNU sed. If current line (pattern space) contains http jump to end of script (b). Otherwise do search and replace.



          sed '/http/b; s/.md#/.html#/' file


          Output:




          1.test.html#
          2.test.html#
          3.http://test.html#
          4.https://test.md#
          5.http://test.md#
          6.test2.html#


          If you want to edit your file "in place" use sed's option -i.




          See: man sed






          share|improve this answer




















          • 4





            Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

            – John1024
            Aug 4 at 5:49



















          2














          perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
          perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
          perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
          perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files


          sed and awk are great, but perl has everything they have and very much more.






          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%2f533784%2fsearch-and-replace-a-substring-only-if-another-substring-is-not-present%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            7














            With GNU sed. If current line (pattern space) contains http jump to end of script (b). Otherwise do search and replace.



            sed '/http/b; s/.md#/.html#/' file


            Output:




            1.test.html#
            2.test.html#
            3.http://test.html#
            4.https://test.md#
            5.http://test.md#
            6.test2.html#


            If you want to edit your file "in place" use sed's option -i.




            See: man sed






            share|improve this answer




















            • 4





              Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

              – John1024
              Aug 4 at 5:49
















            7














            With GNU sed. If current line (pattern space) contains http jump to end of script (b). Otherwise do search and replace.



            sed '/http/b; s/.md#/.html#/' file


            Output:




            1.test.html#
            2.test.html#
            3.http://test.html#
            4.https://test.md#
            5.http://test.md#
            6.test2.html#


            If you want to edit your file "in place" use sed's option -i.




            See: man sed






            share|improve this answer




















            • 4





              Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

              – John1024
              Aug 4 at 5:49














            7












            7








            7







            With GNU sed. If current line (pattern space) contains http jump to end of script (b). Otherwise do search and replace.



            sed '/http/b; s/.md#/.html#/' file


            Output:




            1.test.html#
            2.test.html#
            3.http://test.html#
            4.https://test.md#
            5.http://test.md#
            6.test2.html#


            If you want to edit your file "in place" use sed's option -i.




            See: man sed






            share|improve this answer













            With GNU sed. If current line (pattern space) contains http jump to end of script (b). Otherwise do search and replace.



            sed '/http/b; s/.md#/.html#/' file


            Output:




            1.test.html#
            2.test.html#
            3.http://test.html#
            4.https://test.md#
            5.http://test.md#
            6.test2.html#


            If you want to edit your file "in place" use sed's option -i.




            See: man sed







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Aug 4 at 5:29









            CyrusCyrus

            7,8762 gold badges12 silver badges41 bronze badges




            7,8762 gold badges12 silver badges41 bronze badges










            • 4





              Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

              – John1024
              Aug 4 at 5:49













            • 4





              Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

              – John1024
              Aug 4 at 5:49








            4




            4





            Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

            – John1024
            Aug 4 at 5:49






            Very good solution. Just as an alternative: sed '/http/! s/.md#/.html#/' file

            – John1024
            Aug 4 at 5:49














            2














            perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
            perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
            perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
            perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files


            sed and awk are great, but perl has everything they have and very much more.






            share|improve this answer































              2














              perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
              perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
              perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
              perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files


              sed and awk are great, but perl has everything they have and very much more.






              share|improve this answer





























                2












                2








                2







                perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
                perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
                perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
                perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files


                sed and awk are great, but perl has everything they have and very much more.






                share|improve this answer















                perl -pe'/http/ or s/.md#/.html#/' input.txt > output.txt
                perl -pe'/http/||s/.md#/.html#/' input.txt > output.txt #same
                perl -i -pe'/http/||s/.md#/.html#/' file.txt #edit inplace, changes file.txt
                perl -i.bk -pe'/http/||s/.md#/.html#/' files*.txt #same with backups to .bk files


                sed and awk are great, but perl has everything they have and very much more.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 5 at 10:55

























                answered Aug 4 at 8:59









                Kjetil S.Kjetil S.

                1755 bronze badges




                1755 bronze badges






























                    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%2f533784%2fsearch-and-replace-a-substring-only-if-another-substring-is-not-present%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

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

                    Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?