-1

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

5
  • 3
    Use sed which is the right tool for your requirements May 8, 2018 at 19:57
  • Use ed which might be a better tool for your requirement May 8, 2018 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, 2018 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. May 8, 2018 at 20:04
  • Cross-post of
    – user232326
    May 9, 2018 at 6:54

2 Answers 2

1

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.
4
  • 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 May 8, 2018 at 22:40
  • 1
    Same with the next one: sed -ir "/^values = / s/ $REMOVE_VALUE,//" file May 8, 2018 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. May 8, 2018 at 22:44
  • @glennjackman Wow, very good observation! Thank you very much, I'll update the answer
    – Iskustvo
    May 8, 2018 at 22:45
0

Using ed you can do the following:

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

and

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

Not the answer you're looking for? Browse other questions tagged .