LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-10-2012, 03:32 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 585

Rep: Reputation: 58
How to use sed with some strings


Hello, I have the following strings which I would like to modify a bit using sed:

Code:
REGEX[myserver.com$]
REGEX[dev.example.org$]
REGEX[client1.local.myserver.com$]
How can I use sed to add to before each "." a backslash "\"?

To look like this:

Code:
REGEX[myserver\.com$]
REGEX[dev\.example\.org$]
REGEX[client1\.local\.myserver\.com$]
NOTE: this are examples only and could be any kind of hostname, but not ip address, of course

Thank you
 
Old 12-10-2012, 03:51 AM   #2
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
so you want to add \ before . ?
sed 's/[.]/\\./g'
But it will not work if REGEX contains dots or [ also.
this code will work with almost any regex, but not fully tested. Probably there is a better solution:
Code:
sed 'h;s/[[][^[]*[]]$/[/;x;s/^.*[[]//;s/[.]/\\./g;H;x;s/\n//g' inputfile

Last edited by pan64; 12-10-2012 at 04:00 AM. Reason: added second/better solution
 
Old 12-10-2012, 06:19 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 585

Original Poster
Rep: Reputation: 58
Yeah, that helped.

Thanks!
 
Old 12-10-2012, 08:29 AM   #4
pan64
Senior Member
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 4,929

Rep: Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306Reputation: 1306
Glad to help you. If you really want to say thanks just press yes (bottom right corner)
 
1 members found this post helpful.
  


Reply


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
Scripting- sed command - replacing strings excluding the lines matching a pattern(s) msriram Linux - General 1 11-05-2012 09:29 AM
[Cygwin, sed] Using filenames as both files and search strings within sed lingh Linux - Newbie 5 10-20-2012 10:38 AM
Appending matching strings to specific lines (sed/bash) suntzu Programming 18 09-08-2012 03:29 PM
suing sed to remove certain list of strings from a file ??? HuMan-BiEnG Linux - Distributions 11 10-13-2010 09:04 AM
Help with sed - replacing strings thulley Linux - Software 4 08-22-2006 10:07 AM


All times are GMT -5. The time now is 05:35 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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration