LinuxQuestions.org
Have you heard the LinuxQuestions.org Podcast?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Tags used in this thread
Popular LQ Tags , , , , , , ,

Reply
 
Thread Tools
Old 02-11-2009, 11:03 AM   #1
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 45
Thanked: 0
Sed replace string problem.


[Log in to get rid of this advertisement]
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
zcrxsir88 is offline  
Tag This Post , , , , , , ,
Reply With Quote
Old 02-11-2009, 11:39 AM   #2
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,132
Thanked: 464
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 ,.*$
colucix is offline     Reply With Quote
Old 02-11-2009, 12:04 PM   #3
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 45
Thanked: 0

Original Poster
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
zcrxsir88 is offline     Reply With Quote
Old 02-11-2009, 12:10 PM   #4
colucix
Guru
 
Registered: Sep 2003
Location: Bologna, Italia
Distribution: OpenSUSE 11.1 CentOS 5.4 VectorLinux 6.0
Posts: 5,132
Thanked: 464
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
colucix is offline     Reply With Quote
Old 02-11-2009, 12:48 PM   #5
zcrxsir88
Member
 
Registered: Oct 2004
Location: Cardiff-by-the-Sea, CA
Distribution: Fedora X & RHEL X.X
Posts: 45
Thanked: 0

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

Thank you!

-Vince
zcrxsir88 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem using sed to replace string in file umk Debian 9 10-28-2009 05:40 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 11:02 PM
sed replace string octeto Programming 4 06-06-2007 03:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 09:42 PM


All times are GMT -5. The time now is 08:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration