Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace only the first occurrence of a character using SED 2

Status
Not open for further replies.

JtheRipper

IS-IT--Management
Oct 4, 2002
274
GB
Hi there,

I have a string that looks something like this (this string does not always look the same):
apps1: d: (backup) failed 10:22:33

I would like to replace only the first occurrence of the ":" with a comma so that it looks as follows:
apps1, d: (backup) failed 10:22:33

I know I can use SED to replace the ":" with ",", but it will replace ALL ":"'s with a comma.

Any ideas?

Thanks,
J.
 
Don't use the g flag in the s[/s] command:
s/:/,/

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks PH!

Who would have thought it will be this simple....
Have a star.

J.
 
You can also specify a number in case it's not the first occurance you want to replace. That is, say you wanted to replace the third occurance...
Code:
s/:/,/3
Hope this helps.
 
Thanks SamBones, did not know that...very usefull.

J.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top