LinuxQuestions.org
Visit the LQ Articles and Editorials section
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



Reply
 
Search this Thread
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: 51

Rep: Reputation: 18
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
 
Old 02-11-2009, 11:39 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 ,.*$
 
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: 51

Original Poster
Rep: Reputation: 18
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
 
Old 02-11-2009, 12:10 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
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: 51

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

Thank you!

-Vince
 
  


Reply

Tags
awk, bash, grep, nmap, replace, script, scripting, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12 02-01-2012 09:39 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 01:49 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration