Script to append a value seperated by comma

I have a file like below. How can I add a new value after moss separated by a comma. This adding script should work in such a way that each time i pass a value it should be added one after the other separated by commas.

hru:122:
hello:123:john,philip,mary,kp,moss
hi:124:
bye:125:

Can anybody please help me in this.

depending on what you want to do, this might be one of them:

nawk -v val='foo' '!/:$/ {$0=$0 "," val}1' myFile

thanks a lot for the reply!!

i want this to add only for "hello".. how to do that...? not generic..!! can anybody please help me to understand the above code too??

---------- Post updated at 10:46 PM ---------- Previous update was at 06:47 AM ----------

myfile

hru:122:
hello:123:john,philip,mary,kp,moss
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep

How to modify the above script just to add the new value only to "hello" not for tata. Script should be some what like, search for hello and add the new value there. Each new values to be separated by a comma.. Sorry for not being so clear in my first post

So if the new value i have is foo, the modified file should be like this..

hru:122:
hello:123:john,philip,mary,kp,moss,foo
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep

I modified the script like this..

awk -v val='foo' '!/:$/ {$0=$0 "," val}1' myfile > myfilenew
cat myfilenew > myfile
rm -rf myfilenew

bu the output im getting is like the following...

the new myfile

hru:122:
hello:123:john,philip,mary,kp,moss,foo
hi:124:
bye:125:
hi:124:
bye:125:
hi:124:
bye:125:
tata:125:pipe,grep,foo

It would be a great help if some one can help me in this.

Have you tried to adjust the code?

Regards

I am not so good in awk. hence not able to understand the above code...!! :-(. hence could not modify it... :frowning:

so, take a quick look at this link: 10 Awk Tips, Tricks and Pitfalls - good coders code, great reuse
hope that helps!

awk -v val='foo' '/^hello:/ {$0=$0 "," val}1' myfile

I got it...!! Thanks for all your help...!!

awk -v val='foo' '/hello:123:/ {$0=$0 "," val}1' myFile