How can I modify a line which contains 2nd occurence of a string?Add a string to a text file from terminalHow...

What are ways to record who took the pictures if a camera is used by multiple people?

Is it rude to ask my opponent to resign an online game when they have a lost endgame?

Divide Numbers by 0

Why do modes sound so different, although they are basically the same as a mode of another scale?

Are there consequences for not filing a DMCA (any country)

Blogging in LaTeX

The 7-numbers crossword

Why are CEOs generally fired rather being demoted?

Could these polynomials be identified?

How to encrypt the .viminfo file and still get Vim to read it?

Polarity of gas discharge tubes?

Why don't "echo -e" commands seem to produce the right output?

Why do we need explainable AI?

Turn off Google Chrome's Notification for "Flash Player will no longer be supported after December 2020."

How could reincarnation magic be limited to prevent overuse?

Tasha's Hideous Laughter used on a deaf person?

Is it good practice to speed up and slow down where not written in a song?

Displaying Time in HH:MM Format

How can I store milk for long periods of time?

D Scale Question

Was there an original and definitive use of alternate dimensions/realities in fiction?

Is there anything in the universe that cannot be compressed?

Are there balance issues when allowing attack of opportunity against any creature?

In Toy Story, are toys the only inanimate objects that become alive? And if so, why?



How can I modify a line which contains 2nd occurence of a string?


Add a string to a text file from terminalHow to add exports to fish like in .bashrc?How can I hibernate/suspend from the command line and do so at a specific timeA command to list all users? And how to add, delete, modify users?How does the system know which paths to search for executables?Comment, Uncomment, change setting based on string, add line from terminalHow do I modify paths and change to a real path?How do I find a single unique line in a file?How to use sed to modify last 3 digit of a line with random number when a string matches in a file






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







2















This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?



I should modify the line eni=10.*10**9 to eni=10.*10**8 note the exponents. It is the second time that eni appears










share|improve this question



























  • Do you mean the exact middle of a file?

    – guillermo chamorro
    10 hours ago











  • No I mean somewhere in the file

    – mattiav27
    10 hours ago











  • If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

    – Kulfy
    10 hours ago











  • @Kulfy it is the second time the term 'eni' appears if that can be useful

    – mattiav27
    10 hours ago








  • 3





    What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

    – Kulfy
    10 hours ago




















2















This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?



I should modify the line eni=10.*10**9 to eni=10.*10**8 note the exponents. It is the second time that eni appears










share|improve this question



























  • Do you mean the exact middle of a file?

    – guillermo chamorro
    10 hours ago











  • No I mean somewhere in the file

    – mattiav27
    10 hours ago











  • If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

    – Kulfy
    10 hours ago











  • @Kulfy it is the second time the term 'eni' appears if that can be useful

    – mattiav27
    10 hours ago








  • 3





    What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

    – Kulfy
    10 hours ago
















2












2








2








This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?



I should modify the line eni=10.*10**9 to eni=10.*10**8 note the exponents. It is the second time that eni appears










share|improve this question
















This post explains how to add a line at the beginning of a file from the terminal. But how do I modify from the terminal a line somewhere in a file if I do not know which line it is?



I should modify the line eni=10.*10**9 to eni=10.*10**8 note the exponents. It is the second time that eni appears







command-line






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 8 hours ago









Kulfy

7,59010 gold badges31 silver badges58 bronze badges




7,59010 gold badges31 silver badges58 bronze badges










asked 10 hours ago









mattiav27mattiav27

1871 gold badge1 silver badge9 bronze badges




1871 gold badge1 silver badge9 bronze badges
















  • Do you mean the exact middle of a file?

    – guillermo chamorro
    10 hours ago











  • No I mean somewhere in the file

    – mattiav27
    10 hours ago











  • If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

    – Kulfy
    10 hours ago











  • @Kulfy it is the second time the term 'eni' appears if that can be useful

    – mattiav27
    10 hours ago








  • 3





    What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

    – Kulfy
    10 hours ago





















  • Do you mean the exact middle of a file?

    – guillermo chamorro
    10 hours ago











  • No I mean somewhere in the file

    – mattiav27
    10 hours ago











  • If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

    – Kulfy
    10 hours ago











  • @Kulfy it is the second time the term 'eni' appears if that can be useful

    – mattiav27
    10 hours ago








  • 3





    What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

    – Kulfy
    10 hours ago



















Do you mean the exact middle of a file?

– guillermo chamorro
10 hours ago





Do you mean the exact middle of a file?

– guillermo chamorro
10 hours ago













No I mean somewhere in the file

– mattiav27
10 hours ago





No I mean somewhere in the file

– mattiav27
10 hours ago













If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

– Kulfy
10 hours ago





If not line number is there any pattern or some kind of phrase which is to be followed? Like add this line before a line which contains "blabla".

– Kulfy
10 hours ago













@Kulfy it is the second time the term 'eni' appears if that can be useful

– mattiav27
10 hours ago







@Kulfy it is the second time the term 'eni' appears if that can be useful

– mattiav27
10 hours ago






3




3





What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

– Kulfy
10 hours ago







What exactly kind of modifications do you want? Please edit your question and try to make it more descriptive and specific.

– Kulfy
10 hours ago












3 Answers
3






active

oldest

votes


















2















I think this is what you want:



line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)

sed -i $line's/9$/8/' file





share|improve this answer




























  • @wjandrea modified the answer, take a look.

    – guillermo chamorro
    8 hours ago






  • 2





    Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

    – wjandrea
    8 hours ago











  • This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

    – Raffa
    7 hours ago






  • 1





    @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

    – guillermo chamorro
    7 hours ago











  • @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

    – Raffa
    7 hours ago



















1















Using sed:



sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename


/ is one of the delimiters and is the escape character. is used so that bash won't interpret special characters as some command, like * as wildcard.






share|improve this answer




























  • This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

    – Raffa
    7 hours ago



















0















Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace module) you could do something like



gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file


You can make the regular expressions /eni=/ and/or /10**9/ more or less specific as required.






share|improve this answer




























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    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: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2faskubuntu.com%2fquestions%2f1170057%2fhow-can-i-modify-a-line-which-contains-2nd-occurence-of-a-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2















    I think this is what you want:



    line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)

    sed -i $line's/9$/8/' file





    share|improve this answer




























    • @wjandrea modified the answer, take a look.

      – guillermo chamorro
      8 hours ago






    • 2





      Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

      – wjandrea
      8 hours ago











    • This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

      – Raffa
      7 hours ago






    • 1





      @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

      – guillermo chamorro
      7 hours ago











    • @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

      – Raffa
      7 hours ago
















    2















    I think this is what you want:



    line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)

    sed -i $line's/9$/8/' file





    share|improve this answer




























    • @wjandrea modified the answer, take a look.

      – guillermo chamorro
      8 hours ago






    • 2





      Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

      – wjandrea
      8 hours ago











    • This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

      – Raffa
      7 hours ago






    • 1





      @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

      – guillermo chamorro
      7 hours ago











    • @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

      – Raffa
      7 hours ago














    2














    2










    2









    I think this is what you want:



    line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)

    sed -i $line's/9$/8/' file





    share|improve this answer















    I think this is what you want:



    line=$(grep -n -m2 "eni" file | tail -n1 | cut -f1 -d:)

    sed -i $line's/9$/8/' file






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 9 hours ago

























    answered 9 hours ago









    guillermo chamorroguillermo chamorro

    6611 silver badge13 bronze badges




    6611 silver badge13 bronze badges
















    • @wjandrea modified the answer, take a look.

      – guillermo chamorro
      8 hours ago






    • 2





      Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

      – wjandrea
      8 hours ago











    • This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

      – Raffa
      7 hours ago






    • 1





      @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

      – guillermo chamorro
      7 hours ago











    • @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

      – Raffa
      7 hours ago



















    • @wjandrea modified the answer, take a look.

      – guillermo chamorro
      8 hours ago






    • 2





      Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

      – wjandrea
      8 hours ago











    • This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

      – Raffa
      7 hours ago






    • 1





      @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

      – guillermo chamorro
      7 hours ago











    • @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

      – Raffa
      7 hours ago

















    @wjandrea modified the answer, take a look.

    – guillermo chamorro
    8 hours ago





    @wjandrea modified the answer, take a look.

    – guillermo chamorro
    8 hours ago




    2




    2





    Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

    – wjandrea
    8 hours ago





    Nice! You could simplify getting the line number: line="$(awk '/eni/ {count+=1;if(count==2){print NR}}' test.txt)"

    – wjandrea
    8 hours ago













    This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

    – Raffa
    7 hours ago





    This answer will match the second occurrence of eni and return the line number. Then it will change the last occurrence of 9 with 8 in that line. If more than one 9 are present in the same line, it will only change the last one and might miss the intended one.

    – Raffa
    7 hours ago




    1




    1





    @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

    – guillermo chamorro
    7 hours ago





    @Raffa Yes, a more elaborate regex could be done, but OP's data doesn't show more text after the line, that's why I chose to simplify it to match what OP shows as a line. Note that OP wrote "I should modify the line", not x content in the line.

    – guillermo chamorro
    7 hours ago













    @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

    – Raffa
    7 hours ago





    @guillermochamorro You did a good job, but I however recommend you add such explanation to your answer as it could prevent unintended modification to the file. Thank you

    – Raffa
    7 hours ago













    1















    Using sed:



    sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename


    / is one of the delimiters and is the escape character. is used so that bash won't interpret special characters as some command, like * as wildcard.






    share|improve this answer




























    • This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

      – Raffa
      7 hours ago
















    1















    Using sed:



    sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename


    / is one of the delimiters and is the escape character. is used so that bash won't interpret special characters as some command, like * as wildcard.






    share|improve this answer




























    • This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

      – Raffa
      7 hours ago














    1














    1










    1









    Using sed:



    sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename


    / is one of the delimiters and is the escape character. is used so that bash won't interpret special characters as some command, like * as wildcard.






    share|improve this answer















    Using sed:



    sed -i ': 1 ; N ; $!b1 ; s/eni=10.*10**9/eni=10.*10**8/2' filename


    / is one of the delimiters and is the escape character. is used so that bash won't interpret special characters as some command, like * as wildcard.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 8 hours ago

























    answered 8 hours ago









    KulfyKulfy

    7,59010 gold badges31 silver badges58 bronze badges




    7,59010 gold badges31 silver badges58 bronze badges
















    • This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

      – Raffa
      7 hours ago



















    • This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

      – Raffa
      7 hours ago

















    This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

    – Raffa
    7 hours ago





    This answer will match the second occurrence of eni=10.*10**9 in a new line and will change it with eni=10.*10**8. Run it only once.

    – Raffa
    7 hours ago











    0















    Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace module) you could do something like



    gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file


    You can make the regular expressions /eni=/ and/or /10**9/ more or less specific as required.






    share|improve this answer






























      0















      Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace module) you could do something like



      gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file


      You can make the regular expressions /eni=/ and/or /10**9/ more or less specific as required.






      share|improve this answer




























        0














        0










        0









        Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace module) you could do something like



        gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file


        You can make the regular expressions /eni=/ and/or /10**9/ more or less specific as required.






        share|improve this answer













        Since Ubuntu now ships with GNU Awk v4.0+ (which provides an inplace module) you could do something like



        gawk -i inplace '/eni=/ {if (++c == 2) sub(/10**9/,"10**8")} 1' file


        You can make the regular expressions /eni=/ and/or /10**9/ more or less specific as required.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 4 hours ago









        steeldriversteeldriver

        78.4k12 gold badges129 silver badges213 bronze badges




        78.4k12 gold badges129 silver badges213 bronze badges

































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • 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%2faskubuntu.com%2fquestions%2f1170057%2fhow-can-i-modify-a-line-which-contains-2nd-occurence-of-a-string%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...

            Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m