Append in shell script [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I have a file test.txt which has a line like below



values = abc, def, dfg, ert, sdf, sfd, sdf 


I use the shell script to add a new value with comma separation in the values in the test,txt.



For example, I need to add the 'ghf' in the test.txt:



values = abc, def, dfg, ert, sdf, sfd, sdf, ghf 


Also I need to remove the value.



For example, if I need to remove the 'ert' in the test.txt:



values = abc, def, dfg, sdf, sfd, sdf, ghf 


How can I achieve this in shell script?



(i.e)



If I give the



echo "abc" >> test.txt


It is able to append the "abc" to the end of the file
but I need to append to the 'values=' field in test.txt



Thanks,

Hari







share|improve this question











closed as too broad by Jeff Schaller, Isaac, Kiwy, Anthony Geoghegan, Archemar May 9 at 15:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 3




    Use sed which is the right tool for your requirements
    – Î±Ò“sнιη
    May 8 at 19:57











  • Use ed which might be a better tool for your requirement
    – Valentin B
    May 8 at 20:02






  • 1




    Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
    – Jeff Schaller
    May 8 at 20:03






  • 1




    How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
    – Andy Dalton
    May 8 at 20:04











  • Cross-post of
    – Isaac
    May 9 at 6:54















up vote
-1
down vote

favorite












I have a file test.txt which has a line like below



values = abc, def, dfg, ert, sdf, sfd, sdf 


I use the shell script to add a new value with comma separation in the values in the test,txt.



For example, I need to add the 'ghf' in the test.txt:



values = abc, def, dfg, ert, sdf, sfd, sdf, ghf 


Also I need to remove the value.



For example, if I need to remove the 'ert' in the test.txt:



values = abc, def, dfg, sdf, sfd, sdf, ghf 


How can I achieve this in shell script?



(i.e)



If I give the



echo "abc" >> test.txt


It is able to append the "abc" to the end of the file
but I need to append to the 'values=' field in test.txt



Thanks,

Hari







share|improve this question











closed as too broad by Jeff Schaller, Isaac, Kiwy, Anthony Geoghegan, Archemar May 9 at 15:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 3




    Use sed which is the right tool for your requirements
    – Î±Ò“sнιη
    May 8 at 19:57











  • Use ed which might be a better tool for your requirement
    – Valentin B
    May 8 at 20:02






  • 1




    Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
    – Jeff Schaller
    May 8 at 20:03






  • 1




    How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
    – Andy Dalton
    May 8 at 20:04











  • Cross-post of
    – Isaac
    May 9 at 6:54













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have a file test.txt which has a line like below



values = abc, def, dfg, ert, sdf, sfd, sdf 


I use the shell script to add a new value with comma separation in the values in the test,txt.



For example, I need to add the 'ghf' in the test.txt:



values = abc, def, dfg, ert, sdf, sfd, sdf, ghf 


Also I need to remove the value.



For example, if I need to remove the 'ert' in the test.txt:



values = abc, def, dfg, sdf, sfd, sdf, ghf 


How can I achieve this in shell script?



(i.e)



If I give the



echo "abc" >> test.txt


It is able to append the "abc" to the end of the file
but I need to append to the 'values=' field in test.txt



Thanks,

Hari







share|improve this question











I have a file test.txt which has a line like below



values = abc, def, dfg, ert, sdf, sfd, sdf 


I use the shell script to add a new value with comma separation in the values in the test,txt.



For example, I need to add the 'ghf' in the test.txt:



values = abc, def, dfg, ert, sdf, sfd, sdf, ghf 


Also I need to remove the value.



For example, if I need to remove the 'ert' in the test.txt:



values = abc, def, dfg, sdf, sfd, sdf, ghf 


How can I achieve this in shell script?



(i.e)



If I give the



echo "abc" >> test.txt


It is able to append the "abc" to the end of the file
but I need to append to the 'values=' field in test.txt



Thanks,

Hari









share|improve this question










share|improve this question




share|improve this question









asked May 8 at 19:48









Harry

1094




1094




closed as too broad by Jeff Schaller, Isaac, Kiwy, Anthony Geoghegan, Archemar May 9 at 15:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as too broad by Jeff Schaller, Isaac, Kiwy, Anthony Geoghegan, Archemar May 9 at 15:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 3




    Use sed which is the right tool for your requirements
    – Î±Ò“sнιη
    May 8 at 19:57











  • Use ed which might be a better tool for your requirement
    – Valentin B
    May 8 at 20:02






  • 1




    Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
    – Jeff Schaller
    May 8 at 20:03






  • 1




    How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
    – Andy Dalton
    May 8 at 20:04











  • Cross-post of
    – Isaac
    May 9 at 6:54













  • 3




    Use sed which is the right tool for your requirements
    – Î±Ò“sнιη
    May 8 at 19:57











  • Use ed which might be a better tool for your requirement
    – Valentin B
    May 8 at 20:02






  • 1




    Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
    – Jeff Schaller
    May 8 at 20:03






  • 1




    How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
    – Andy Dalton
    May 8 at 20:04











  • Cross-post of
    – Isaac
    May 9 at 6:54








3




3




Use sed which is the right tool for your requirements
– Î±Ò“sнιη
May 8 at 19:57





Use sed which is the right tool for your requirements
– Î±Ò“sнιη
May 8 at 19:57













Use ed which might be a better tool for your requirement
– Valentin B
May 8 at 20:02




Use ed which might be a better tool for your requirement
– Valentin B
May 8 at 20:02




1




1




Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
– Jeff Schaller
May 8 at 20:03




Are these ert and ghf values one-time (static) changes, or does the script need to prompt for add/remove and the value?
– Jeff Schaller
May 8 at 20:03




1




1




How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
– Andy Dalton
May 8 at 20:04





How do you know when you want to remove ert? I don't see an example of how you'd expect to do that.
– Andy Dalton
May 8 at 20:04













Cross-post of
– Isaac
May 9 at 6:54





Cross-post of
– Isaac
May 9 at 6:54











2 Answers
2






active

oldest

votes

















up vote
1
down vote













Fast solution without a guard from corner cases.

(Thanks glenn jackman for sed optimizations.)



Adding



sed -i "/^values = / s/$/, $NEW_VALUE/" test.txt



Explanation



sed -i - Make changes in the given file, do not print to the stdout.



/^values = / - When sed is positioned on the line that starts with values =.



s/$/, $NEW_VALUE/ - Append , $NEW_VALUE at the end of the line.



test.txt - File on which sed will perform actions.



Removing



sed -i "/^values = / s/ $REMOVE_VALUE,?//" test.txt



Explanation



s/ $REMOVE_VALUE,?// - Remove the first occurence of $REMOVE_VALUE along with optional following ,.



Problems with this solution



  1. Adding the first value will add , right after values =

  2. Removing the last value will leave the line ending with ,.

  3. If added/removed variables containt sed's separator for s command (in my example it is /), command won't work.





share|improve this answer



















  • 1




    good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
    – glenn jackman
    May 8 at 22:40






  • 1




    Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
    – glenn jackman
    May 8 at 22:43






  • 1




    In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
    – glenn jackman
    May 8 at 22:44










  • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
    – Iskustvo
    May 8 at 22:45


















up vote
0
down vote













Using ed you can do the following:



ed -s test.txt <<<$'/values/s/$/,ghfnw' # add ghf to the end of line


and



ed -s values <<<$'/values/s/ert,//nw' # remove ert





share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    Fast solution without a guard from corner cases.

    (Thanks glenn jackman for sed optimizations.)



    Adding



    sed -i "/^values = / s/$/, $NEW_VALUE/" test.txt



    Explanation



    sed -i - Make changes in the given file, do not print to the stdout.



    /^values = / - When sed is positioned on the line that starts with values =.



    s/$/, $NEW_VALUE/ - Append , $NEW_VALUE at the end of the line.



    test.txt - File on which sed will perform actions.



    Removing



    sed -i "/^values = / s/ $REMOVE_VALUE,?//" test.txt



    Explanation



    s/ $REMOVE_VALUE,?// - Remove the first occurence of $REMOVE_VALUE along with optional following ,.



    Problems with this solution



    1. Adding the first value will add , right after values =

    2. Removing the last value will leave the line ending with ,.

    3. If added/removed variables containt sed's separator for s command (in my example it is /), command won't work.





    share|improve this answer



















    • 1




      good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
      – glenn jackman
      May 8 at 22:40






    • 1




      Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
      – glenn jackman
      May 8 at 22:43






    • 1




      In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
      – glenn jackman
      May 8 at 22:44










    • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
      – Iskustvo
      May 8 at 22:45















    up vote
    1
    down vote













    Fast solution without a guard from corner cases.

    (Thanks glenn jackman for sed optimizations.)



    Adding



    sed -i "/^values = / s/$/, $NEW_VALUE/" test.txt



    Explanation



    sed -i - Make changes in the given file, do not print to the stdout.



    /^values = / - When sed is positioned on the line that starts with values =.



    s/$/, $NEW_VALUE/ - Append , $NEW_VALUE at the end of the line.



    test.txt - File on which sed will perform actions.



    Removing



    sed -i "/^values = / s/ $REMOVE_VALUE,?//" test.txt



    Explanation



    s/ $REMOVE_VALUE,?// - Remove the first occurence of $REMOVE_VALUE along with optional following ,.



    Problems with this solution



    1. Adding the first value will add , right after values =

    2. Removing the last value will leave the line ending with ,.

    3. If added/removed variables containt sed's separator for s command (in my example it is /), command won't work.





    share|improve this answer



















    • 1




      good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
      – glenn jackman
      May 8 at 22:40






    • 1




      Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
      – glenn jackman
      May 8 at 22:43






    • 1




      In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
      – glenn jackman
      May 8 at 22:44










    • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
      – Iskustvo
      May 8 at 22:45













    up vote
    1
    down vote










    up vote
    1
    down vote









    Fast solution without a guard from corner cases.

    (Thanks glenn jackman for sed optimizations.)



    Adding



    sed -i "/^values = / s/$/, $NEW_VALUE/" test.txt



    Explanation



    sed -i - Make changes in the given file, do not print to the stdout.



    /^values = / - When sed is positioned on the line that starts with values =.



    s/$/, $NEW_VALUE/ - Append , $NEW_VALUE at the end of the line.



    test.txt - File on which sed will perform actions.



    Removing



    sed -i "/^values = / s/ $REMOVE_VALUE,?//" test.txt



    Explanation



    s/ $REMOVE_VALUE,?// - Remove the first occurence of $REMOVE_VALUE along with optional following ,.



    Problems with this solution



    1. Adding the first value will add , right after values =

    2. Removing the last value will leave the line ending with ,.

    3. If added/removed variables containt sed's separator for s command (in my example it is /), command won't work.





    share|improve this answer















    Fast solution without a guard from corner cases.

    (Thanks glenn jackman for sed optimizations.)



    Adding



    sed -i "/^values = / s/$/, $NEW_VALUE/" test.txt



    Explanation



    sed -i - Make changes in the given file, do not print to the stdout.



    /^values = / - When sed is positioned on the line that starts with values =.



    s/$/, $NEW_VALUE/ - Append , $NEW_VALUE at the end of the line.



    test.txt - File on which sed will perform actions.



    Removing



    sed -i "/^values = / s/ $REMOVE_VALUE,?//" test.txt



    Explanation



    s/ $REMOVE_VALUE,?// - Remove the first occurence of $REMOVE_VALUE along with optional following ,.



    Problems with this solution



    1. Adding the first value will add , right after values =

    2. Removing the last value will leave the line ending with ,.

    3. If added/removed variables containt sed's separator for s command (in my example it is /), command won't work.






    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited May 8 at 23:22


























    answered May 8 at 20:11









    Iskustvo

    667118




    667118







    • 1




      good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
      – glenn jackman
      May 8 at 22:40






    • 1




      Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
      – glenn jackman
      May 8 at 22:43






    • 1




      In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
      – glenn jackman
      May 8 at 22:44










    • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
      – Iskustvo
      May 8 at 22:45













    • 1




      good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
      – glenn jackman
      May 8 at 22:40






    • 1




      Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
      – glenn jackman
      May 8 at 22:43






    • 1




      In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
      – glenn jackman
      May 8 at 22:44










    • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
      – Iskustvo
      May 8 at 22:45








    1




    1




    good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
    – glenn jackman
    May 8 at 22:40




    good suggestion. However you are making sed do too much work: there's no need to match everything only to put it into the replacement. Just match the end-of-line and add the new value there. Also, you need double quotes for the shell to expand the variable: sed -i "/^values = / s/$/$NEW_VALUE/" file
    – glenn jackman
    May 8 at 22:40




    1




    1




    Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
    – glenn jackman
    May 8 at 22:43




    Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file
    – glenn jackman
    May 8 at 22:43




    1




    1




    In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
    – glenn jackman
    May 8 at 22:44




    In general, you don't need .* at the beginning or end of the pattern if you're going to just put them into the replacement unchanged.
    – glenn jackman
    May 8 at 22:44












    @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
    – Iskustvo
    May 8 at 22:45





    @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
    – Iskustvo
    May 8 at 22:45













    up vote
    0
    down vote













    Using ed you can do the following:



    ed -s test.txt <<<$'/values/s/$/,ghfnw' # add ghf to the end of line


    and



    ed -s values <<<$'/values/s/ert,//nw' # remove ert





    share|improve this answer

























      up vote
      0
      down vote













      Using ed you can do the following:



      ed -s test.txt <<<$'/values/s/$/,ghfnw' # add ghf to the end of line


      and



      ed -s values <<<$'/values/s/ert,//nw' # remove ert





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Using ed you can do the following:



        ed -s test.txt <<<$'/values/s/$/,ghfnw' # add ghf to the end of line


        and



        ed -s values <<<$'/values/s/ert,//nw' # remove ert





        share|improve this answer













        Using ed you can do the following:



        ed -s test.txt <<<$'/values/s/$/,ghfnw' # add ghf to the end of line


        and



        ed -s values <<<$'/values/s/ert,//nw' # remove ert






        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 8 at 20:07









        Valentin B

        5,48111527




        5,48111527












            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay