LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 04:32 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 587

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, 04:51 AM   #2
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,167

Rep: Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364Reputation: 1364
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 05:00 AM. Reason: added second/better solution
 
Old 12-10-2012, 07:19 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 587

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

Thanks!
 
Old 12-10-2012, 09:29 AM   #4
pan64
Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 5,167

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


All times are GMT -5. The time now is 05:19 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