LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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, 10: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, 10:39 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 11:04 AM   #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, 11:10 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 11:48 AM   #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



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 08:39 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM
sed replace string octeto Programming 4 06-06-2007 02:09 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 10:58 AM
[sed] replace string? chuanyung Programming 3 03-11-2004 08:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:02 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
Open Source Consulting | Domain Registration