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

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

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

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form