-2

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?

EDITED :

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

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Harry
  • 3,072
  • 6
  • 43
  • 100

1 Answers1

0

There a several ways to accomplish this, but here are two simple sed commands that should get you started. First, to append the string ", grt" to the end of the line beginning with "values = " in the file test.txt:

sed -e "s/\(values = .*$\)/\1, ghf/" < test.txt

Second, to remove the string ", grt" from the line beginning with "values = " in the same file:

sed -e "s/\(values = .*\), grt\(.*$\)/\1\2/" < test.txt

These are just examples -- for example, the second command will fail if the string "grt" is the first string after the equals sign -- but hopefully they will get you started.

Leslie
  • 618
  • 4
  • 14