11. Given a file, replace all occurrence of word “DEF” with “ABC” from 5th line till end in only...

Why is the T-1000 humanoid?

In what state are satellites left in when they are left in a graveyard orbit?

Which is the current decimal separator?

How do I get rid of distortion in pictures of distant objects photographed with a telephoto lens?

How to publish superseding results without creating enemies

Why some files are not movable in Windows 10

Bash, import output from command as command

Why is my fire extinguisher emptied after one use?

Is "you will become a subject matter expert" code for "you'll be working on your own 100% of the time"?

What is my breathable atmosphere composed of?

My research paper filed as a patent in China by my Chinese supervisor without me as inventor

Will the UK home office know about 5 previous visa rejections in other countries?

What officially disallows US presidents from driving?

Can I conceal an antihero's insanity - and should I?

5e Level 1 Druid cantrips

What is the mathematical notation for rounding a given number to the nearest integer?

Can I toggle Do Not Disturb on/off on my Mac as easily as I can on my iPhone?

Should you only use colons and periods in dialogues?

What was the ultimate objective of The Party in 1984?

Origin of the term "sinc" function

"Literally" Vs "In the true sense of the word"

What is the derivative of an exponential function with another function as its base?

Some Prime Peerage

Why is this weapon searching for a new owner?



11. Given a file, replace all occurrence of word “DEF” with “ABC” from 5th line till end in only those lines that contains word “MNO”


How to sed only that lines that contains given string?sed - change lines that start with one given word and end with another?Reading a string till a key word and replacing from there with another stringBash find and replace in a C++ fileRewrite a find command that uses sed -i for AIXBash - Integer expression expectedshell script to test condition on passed stringSubstitution of a line with another line in multiple filessed command to replace and write






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







-1















enter image description here



I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!










share|improve this question







New contributor



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

















  • 1





    Please don't post screenshots of text. Copy the text here and use code formatting instead

    – muru
    12 mins ago


















-1















enter image description here



I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!










share|improve this question







New contributor



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

















  • 1





    Please don't post screenshots of text. Copy the text here and use code formatting instead

    – muru
    12 mins ago














-1












-1








-1








enter image description here



I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!










share|improve this question







New contributor



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











enter image description here



I was able to substitute all occurrences of "DEF" string with "ABC" string starting from line 5 in my file called "DEFABC.txt". However, when I try to add another condition with an if statement (only substitute if the line contains "MNO" string) the script still substitutes all occurrences after line 5. What am I doing wrong? Thank you!







shell-script sed






share|improve this question







New contributor



Kristina 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



Kristina 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






New contributor



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








asked 20 mins ago









KristinaKristina

1




1




New contributor



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




New contributor




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













  • 1





    Please don't post screenshots of text. Copy the text here and use code formatting instead

    – muru
    12 mins ago














  • 1





    Please don't post screenshots of text. Copy the text here and use code formatting instead

    – muru
    12 mins ago








1




1





Please don't post screenshots of text. Copy the text here and use code formatting instead

– muru
12 mins ago





Please don't post screenshots of text. Copy the text here and use code formatting instead

– muru
12 mins ago










1 Answer
1






active

oldest

votes


















0
















sed can do this on its own:



sed -e '5,${/MNO/{s/DEF/ABC/g;};}'


This:




  1. Selects only lines from 5 to the end to act on, with the code in the outer braces.

  2. Then further selects only those lines from that set containing MNO, to act on with the code in the inner braces.

  3. Finally replaces DEF with ABC on those lines.


All other lines are printed unchanged.






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/4.0/"u003ecc by-sa 4.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
    });


    }
    });







    Kristina 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%2f541508%2f11-given-a-file-replace-all-occurrence-of-word-def-with-abc-from-5th-line%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0
















    sed can do this on its own:



    sed -e '5,${/MNO/{s/DEF/ABC/g;};}'


    This:




    1. Selects only lines from 5 to the end to act on, with the code in the outer braces.

    2. Then further selects only those lines from that set containing MNO, to act on with the code in the inner braces.

    3. Finally replaces DEF with ABC on those lines.


    All other lines are printed unchanged.






    share|improve this answer






























      0
















      sed can do this on its own:



      sed -e '5,${/MNO/{s/DEF/ABC/g;};}'


      This:




      1. Selects only lines from 5 to the end to act on, with the code in the outer braces.

      2. Then further selects only those lines from that set containing MNO, to act on with the code in the inner braces.

      3. Finally replaces DEF with ABC on those lines.


      All other lines are printed unchanged.






      share|improve this answer




























        0














        0










        0









        sed can do this on its own:



        sed -e '5,${/MNO/{s/DEF/ABC/g;};}'


        This:




        1. Selects only lines from 5 to the end to act on, with the code in the outer braces.

        2. Then further selects only those lines from that set containing MNO, to act on with the code in the inner braces.

        3. Finally replaces DEF with ABC on those lines.


        All other lines are printed unchanged.






        share|improve this answer













        sed can do this on its own:



        sed -e '5,${/MNO/{s/DEF/ABC/g;};}'


        This:




        1. Selects only lines from 5 to the end to act on, with the code in the outer braces.

        2. Then further selects only those lines from that set containing MNO, to act on with the code in the inner braces.

        3. Finally replaces DEF with ABC on those lines.


        All other lines are printed unchanged.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 10 mins ago









        Michael HomerMichael Homer

        55.3k9 gold badges155 silver badges189 bronze badges




        55.3k9 gold badges155 silver badges189 bronze badges


























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










            draft saved

            draft discarded

















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













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












            Kristina 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%2f541508%2f11-given-a-file-replace-all-occurrence-of-word-def-with-abc-from-5th-line%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

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...