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

What is the definition of Product

Received email from ISP saying one of my devices has malware

Can authors email you PDFs of their textbook for free?

Divide Numbers by 0

Do universities maintain secret textbooks?

Calculate Landau's function

Using font to highlight a god's speech in dialogue

How to run a command 1 out of N times in Bash

Blogging in LaTeX

How can I improve my formal definitions?

An alternative to "two column" geometry proofs

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

Get rows that exist exactly once per day for a given period

Table alignment (make the content centre)

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

Doesn't the concept of marginal utility speak to a cardinal utility function?

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

Is there anything in the universe that cannot be compressed?

Polarity of gas discharge tubes?

Could these polynomials be identified?

How were US credit cards verified in-store in the 1980's?

How would a disabled person earn their living in a medieval-type town?

Can a human variant take proficiency in initiative?

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



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











  • No I mean somewhere in the file

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











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

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











  • No I mean somewhere in the file

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











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

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









Kulfy

7,59010 gold badges31 silver badges58 bronze badges




7,59010 gold badges31 silver badges58 bronze badges










asked 8 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
    8 hours ago











  • No I mean somewhere in the file

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











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

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


















  • Do you mean the exact middle of a file?

    – guillermo chamorro
    8 hours ago











  • No I mean somewhere in the file

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











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

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

















Do you mean the exact middle of a file?

– guillermo chamorro
8 hours ago





Do you mean the exact middle of a file?

– guillermo chamorro
8 hours ago













No I mean somewhere in the file

– mattiav27
8 hours ago





No I mean somewhere in the file

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













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

– mattiav27
8 hours ago






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

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






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

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






  • 2





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

    – wjandrea
    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
    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
    5 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
      7 hours ago






    • 2





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

      – wjandrea
      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
      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
      5 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
      7 hours ago






    • 2





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

      – wjandrea
      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
      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
      5 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 7 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
      7 hours ago






    • 2





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

      – wjandrea
      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
      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
      5 hours ago

















    • @wjandrea modified the answer, take a look.

      – guillermo chamorro
      7 hours ago






    • 2





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

      – wjandrea
      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
      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
      5 hours ago
















    @wjandrea modified the answer, take a look.

    – guillermo chamorro
    7 hours ago





    @wjandrea modified the answer, take a look.

    – guillermo chamorro
    7 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
    7 hours ago





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

    – wjandrea
    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
    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
    5 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
    5 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 7 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 3 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

            ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

            Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

            19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу