replace a string from a specified line number and column












0














I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427


so the new file should look like



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427


currently i do know how to use sed command to replace K from line 4 with exc such as



sed -i "4s/K/exc/g" <newfile> 


but I do not know how to specify which column to select










share|improve this question






















  • I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
    – Dimitris Mintis
    Dec 9 at 22:30










  • OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
    – G-Man
    Dec 10 at 1:14
















0














I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427


so the new file should look like



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427


currently i do know how to use sed command to replace K from line 4 with exc such as



sed -i "4s/K/exc/g" <newfile> 


but I do not know how to specify which column to select










share|improve this question






















  • I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
    – Dimitris Mintis
    Dec 9 at 22:30










  • OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
    – G-Man
    Dec 10 at 1:14














0












0








0







I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427


so the new file should look like



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427


currently i do know how to use sed command to replace K from line 4 with exc such as



sed -i "4s/K/exc/g" <newfile> 


but I do not know how to specify which column to select










share|improve this question













I would like to replace from line number 4, the character "K" which is right after 19823 but not the "K" character which follows



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823K K60024 8.427


so the new file should look like



19822SOL     OW60021   7.173   
19822SOL HW160022 7.230
19822SOL HW260023 7.124
19823exc K60024 8.427


currently i do know how to use sed command to replace K from line 4 with exc such as



sed -i "4s/K/exc/g" <newfile> 


but I do not know how to specify which column to select







awk sed






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 9 at 22:17









Dimitris Mintis

425




425












  • I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
    – Dimitris Mintis
    Dec 9 at 22:30










  • OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
    – G-Man
    Dec 10 at 1:14


















  • I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
    – Dimitris Mintis
    Dec 9 at 22:30










  • OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
    – G-Man
    Dec 10 at 1:14
















I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30




I can use sed -i "4s/19823K /19823exc/g" <newfile> but I want to avoid this ideally
– Dimitris Mintis
Dec 9 at 22:30












OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
– G-Man
Dec 10 at 1:14




OK, so you don't want to change a K that occurs after a 19823; you want to change a K that occurs in the first column of line 4. Or do you want to change all the Ks that occur in the first column of line 4? Or do you want to change a K that occurs in a specified column of line 4 (or some other specified line)? The simple solution of leaving off the g doesn't help if you want to manipulate a column other than the first.
– G-Man
Dec 10 at 1:14










2 Answers
2






active

oldest

votes


















3














If it's just about that particular K, then getting rid of the g flag will do, as sed will only replace K once. Thus:



sed -i "4s/K/exc/" <newfile> 


Namely, it replaces the first appearance of K with exc on line 4.






share|improve this answer





























    1














    If you want the first K, just get rid of the g flag:



    sed -i '4s/K/exc/g' <newfile> 


    If you want to replace the 2nd, 3rd, etc. K, use 2, 3, 4 instead of g



    sed -i '4s/K/exc/2' <newfile>
    sed -i '4s/K/exc/3' <newfile>


    If you want to replace all Ks from the 3rd up to the end:



    echo 'KKKKKK' | sed s/K/E/3g
    KKEEEE


    And btw, don't use double quotes (") unless you really want to insert shell variables, command expansions, etc in the string passed to sed.






    share|improve this answer





















    • Note that using both a number and g as flags to the substitute command requires GNU sed.
      – Kusalananda
      Dec 15 at 10:20











    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%2f487006%2freplace-a-string-from-a-specified-line-number-and-column%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









    3














    If it's just about that particular K, then getting rid of the g flag will do, as sed will only replace K once. Thus:



    sed -i "4s/K/exc/" <newfile> 


    Namely, it replaces the first appearance of K with exc on line 4.






    share|improve this answer


























      3














      If it's just about that particular K, then getting rid of the g flag will do, as sed will only replace K once. Thus:



      sed -i "4s/K/exc/" <newfile> 


      Namely, it replaces the first appearance of K with exc on line 4.






      share|improve this answer
























        3












        3








        3






        If it's just about that particular K, then getting rid of the g flag will do, as sed will only replace K once. Thus:



        sed -i "4s/K/exc/" <newfile> 


        Namely, it replaces the first appearance of K with exc on line 4.






        share|improve this answer












        If it's just about that particular K, then getting rid of the g flag will do, as sed will only replace K once. Thus:



        sed -i "4s/K/exc/" <newfile> 


        Namely, it replaces the first appearance of K with exc on line 4.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 9 at 22:41









        Tomasz

        9,18852965




        9,18852965

























            1














            If you want the first K, just get rid of the g flag:



            sed -i '4s/K/exc/g' <newfile> 


            If you want to replace the 2nd, 3rd, etc. K, use 2, 3, 4 instead of g



            sed -i '4s/K/exc/2' <newfile>
            sed -i '4s/K/exc/3' <newfile>


            If you want to replace all Ks from the 3rd up to the end:



            echo 'KKKKKK' | sed s/K/E/3g
            KKEEEE


            And btw, don't use double quotes (") unless you really want to insert shell variables, command expansions, etc in the string passed to sed.






            share|improve this answer





















            • Note that using both a number and g as flags to the substitute command requires GNU sed.
              – Kusalananda
              Dec 15 at 10:20
















            1














            If you want the first K, just get rid of the g flag:



            sed -i '4s/K/exc/g' <newfile> 


            If you want to replace the 2nd, 3rd, etc. K, use 2, 3, 4 instead of g



            sed -i '4s/K/exc/2' <newfile>
            sed -i '4s/K/exc/3' <newfile>


            If you want to replace all Ks from the 3rd up to the end:



            echo 'KKKKKK' | sed s/K/E/3g
            KKEEEE


            And btw, don't use double quotes (") unless you really want to insert shell variables, command expansions, etc in the string passed to sed.






            share|improve this answer





















            • Note that using both a number and g as flags to the substitute command requires GNU sed.
              – Kusalananda
              Dec 15 at 10:20














            1












            1








            1






            If you want the first K, just get rid of the g flag:



            sed -i '4s/K/exc/g' <newfile> 


            If you want to replace the 2nd, 3rd, etc. K, use 2, 3, 4 instead of g



            sed -i '4s/K/exc/2' <newfile>
            sed -i '4s/K/exc/3' <newfile>


            If you want to replace all Ks from the 3rd up to the end:



            echo 'KKKKKK' | sed s/K/E/3g
            KKEEEE


            And btw, don't use double quotes (") unless you really want to insert shell variables, command expansions, etc in the string passed to sed.






            share|improve this answer












            If you want the first K, just get rid of the g flag:



            sed -i '4s/K/exc/g' <newfile> 


            If you want to replace the 2nd, 3rd, etc. K, use 2, 3, 4 instead of g



            sed -i '4s/K/exc/2' <newfile>
            sed -i '4s/K/exc/3' <newfile>


            If you want to replace all Ks from the 3rd up to the end:



            echo 'KKKKKK' | sed s/K/E/3g
            KKEEEE


            And btw, don't use double quotes (") unless you really want to insert shell variables, command expansions, etc in the string passed to sed.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Dec 10 at 0:32









            Uncle Billy

            1244




            1244












            • Note that using both a number and g as flags to the substitute command requires GNU sed.
              – Kusalananda
              Dec 15 at 10:20


















            • Note that using both a number and g as flags to the substitute command requires GNU sed.
              – Kusalananda
              Dec 15 at 10:20
















            Note that using both a number and g as flags to the substitute command requires GNU sed.
            – Kusalananda
            Dec 15 at 10:20




            Note that using both a number and g as flags to the substitute command requires GNU sed.
            – Kusalananda
            Dec 15 at 10:20


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f487006%2freplace-a-string-from-a-specified-line-number-and-column%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

            Morgemoulin

            Scott Moir

            Souastre