LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Sed replace string problem. (http://www.linuxquestions.org/questions/programming-9/sed-replace-string-problem-703894/)

zcrxsir88 02-11-2009 10:03 AM

Sed replace string problem.
 
Greetings all,

I'm having some trouble figuring out how to do replace a single character in some output I'm working with. I'm pretty sure sed will take care of it for me, but I haven't quite figured it out.

The data looks as such.

192.168.1.7.68,>,192.168.1.1.67,UDP
192.168.1.7.68,>,192.168.1.1.67,UDP
192.168.1.7.68,>,192.168.1.1.67,UDP
192.168.1.7.68,>,192.168.1.1.67,UDP
192.168.1.7.68,>,192.168.1.1.67,UDP


But I need it to look like this.

192.168.1.7:68,>,192.168.1.1:67,UDP
192.168.1.7:68,>,192.168.1.1:67,UDP
192.168.1.7:68,>,192.168.1.1:67,UDP
192.168.1.7:68,>,192.168.1.1:67,UDP
192.168.1.7:68,>,192.168.1.1:67,UDP


If anyone could help I would greatly appreciate it!

Thanks,

-Vince

colucix 02-11-2009 10:39 AM

Code:

sed -i.bck 's/\(.*\)[.]\([0-9]*,.*\)$/\1:\2/g' file
the regular expression matches any string .* plus a literal dot [.] plus any number [0-9]* followed by a comma plus any string to the end of the line ,.*$

zcrxsir88 02-11-2009 11:04 AM

Thanks!
 
colucix,

Thank you for the quick reply. Everything works great. I did notice that I have to run the string multiple times to get both the :'s in there. Which is completely not a big deal, I was just curious if there was a way to make them do both at the same time?

Thanks again!

-Vince

colucix 02-11-2009 11:10 AM

Ops... I didn't notice the first one. Here is another command
Code:

sed -i.bck 's/\(.*\)[.]\([0-9]*,>,.*\)[.]\([0-9]*,.*\)$/\1:\2:\3/g' file

zcrxsir88 02-11-2009 11:48 AM

Thanks again! I figured it was something like that but couldn't get it to work properly.

Thank you!

-Vince


All times are GMT -5. The time now is 04:42 PM.