How can I append a string at the end of a line in a file

Hi, guys. I have one question:

I have a file called "group", the contents of it is below:

********************************
...
test:x:203:
sales:x:204:
repair:x:205:
research:x:206:brownj
...
***********

Now I want to add string ",sherrys" at the end of "research:x:206:brownj", so the result should be "research:x:206:brownj,sherrys".

My code is below:

added_string=",sherrys"
sed "s/research:x:206:*/&${added_string}/" group

However, it doesn't work properly, it gives this result "research:x:206:,sherrysbrownj"

Does anybody know how to fix my problem?

Thank you very much for your time in advance

-Keyang

sed "s/research:x:206*/&${added_string}/" group

Try this,

cheers

sed "/^research:/ s/$/,$added_string/" group

Hi, daptal. I will miss the ":" after 206 if I used this code :slight_smile:

-Keyang

Hi, cfajohnson.

The code works well. Thanks for the reply. Again, I am very confused.. :slight_smile:

-Keyang

sed # call sed
" # use double quote so the variable "added_string" gets expanded
/^research:/ # find a line that begins with "research:"
s/$/,$added_string/ # substitute end of line with comma and variable "added_string"
" # end double quote to complete the command
group # in file group

[code]
sed "/^research:/ s/$/,$added_string/" group
[/code/