Append in shell script [closed]
Clash 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
linux bash shell-script shell
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.
add a comment |Â
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
linux bash shell-script shell
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
Usesed
which is the right tool for your requirements
â Ã±ÃÂsýù÷
May 8 at 19:57
Useed
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 removeert
? 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
add a comment |Â
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
linux bash shell-script shell
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
linux bash shell-script shell
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
Usesed
which is the right tool for your requirements
â Ã±ÃÂsýù÷
May 8 at 19:57
Useed
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 removeert
? 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
add a comment |Â
3
Usesed
which is the right tool for your requirements
â Ã±ÃÂsýù÷
May 8 at 19:57
Useed
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 removeert
? 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
add a comment |Â
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
- Adding the first value will add
,
right aftervalues =
- Removing the last value will leave the line ending with
,
. - If added/removed variables containt
sed
's separator fors
command (in my example it is/
), command won't work.
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
add a comment |Â
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
add a comment |Â
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
- Adding the first value will add
,
right aftervalues =
- Removing the last value will leave the line ending with
,
. - If added/removed variables containt
sed
's separator fors
command (in my example it is/
), command won't work.
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
add a comment |Â
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
- Adding the first value will add
,
right aftervalues =
- Removing the last value will leave the line ending with
,
. - If added/removed variables containt
sed
's separator fors
command (in my example it is/
), command won't work.
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
add a comment |Â
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
- Adding the first value will add
,
right aftervalues =
- Removing the last value will leave the line ending with
,
. - If added/removed variables containt
sed
's separator fors
command (in my example it is/
), command won't work.
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
- Adding the first value will add
,
right aftervalues =
- Removing the last value will leave the line ending with
,
. - If added/removed variables containt
sed
's separator fors
command (in my example it is/
), command won't work.
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered May 8 at 20:07
Valentin B
5,48111527
5,48111527
add a comment |Â
add a comment |Â
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