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

Why wasn't Linda Hamilton in T3?

Divide Numbers by 0

Displaying Time in HH:MM Format

Missing $ inserted. Extra }, or forgotten $. Missing } inserted

How can I portray a character with no fear of death, without them sounding utterly bored?

Does the telecom provider need physical access to the SIM card to clone it?

How to solve this inequality , when there is a irrational power?

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

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

Different past tense for various *et words

How to use a tikzpicture as a node shape

Blogging in LaTeX

Is there anything in the universe that cannot be compressed?

How are the cards determined in an incomplete deck of many things?

Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code

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

What is the definition of Product

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

Killing task by name - start menu shortcut

Can authors email you PDFs of their textbook for free?

Datasets of Large Molecules

Colored grid with coordinates on all sides?

What is the maximal acceptable delay between pilot's input and flight control surface actuation?

Using large parts of a research paper



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
    9 hours ago











  • No I mean somewhere in the file

    – mattiav27
    9 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
    9 hours ago











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

    – mattiav27
    9 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
    9 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
    9 hours ago











  • No I mean somewhere in the file

    – mattiav27
    9 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
    9 hours ago











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

    – mattiav27
    9 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
    9 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 9 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
    9 hours ago











  • No I mean somewhere in the file

    – mattiav27
    9 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
    9 hours ago











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

    – mattiav27
    9 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
    9 hours ago


















  • Do you mean the exact middle of a file?

    – guillermo chamorro
    9 hours ago











  • No I mean somewhere in the file

    – mattiav27
    9 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
    9 hours ago











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

    – mattiav27
    9 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
    9 hours ago

















Do you mean the exact middle of a file?

– guillermo chamorro
9 hours ago





Do you mean the exact middle of a file?

– guillermo chamorro
9 hours ago













No I mean somewhere in the file

– mattiav27
9 hours ago





No I mean somewhere in the file

– mattiav27
9 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
9 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
9 hours ago













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

– mattiav27
9 hours ago






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

– mattiav27
9 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
9 hours ago






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

– Kulfy
9 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
    6 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
    6 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
    6 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
    6 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
      6 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
      6 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
      6 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
      6 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
      6 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
      6 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 8 hours ago

























    answered 8 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
      6 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
      6 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
      6 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
      6 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
      6 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
      6 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
    6 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
    6 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
    6 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
    6 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
    6 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
    6 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
      6 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
      6 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 7 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
      6 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
      6 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
    6 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
    6 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

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單