LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-17-2012, 09:16 AM   #1
x0000000009
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Rep: Reputation: 0
sed match domain help


I'm trying to come up with a sed pattern match that works to get me the top domain for filtering in procmail.

if the domain is 123.mydomain.org then i want to extract mydomain.org like
sed -e 's/[a-zA-Z0-9]*\.//;s/*\.[a-z]// ;s/ //g'

but if it is just mydomain.org then i would like it to either return mydomain.org or nothing is also fine.

but if it is 123.456.mydomain.com for example it returns
mydomain.com

I am still grasping sed but i think its suppose to be something like
sed -n 's/[A-Za-z0-9]+\(.*\)+.[a-zA-Z]{2,4}/\1/p'
however that does not work.
 
Old 04-17-2012, 10:39 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Have you thought of using awk and setting the delimiter to a dot? Or are we stuck with using sed?
 
Old 04-17-2012, 10:46 AM   #3
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:
$ echo 123.456.mydomain.com | sed -r 's/.*\.([^.]+\.[^.]+)$/\1/'
mydomain.com
$ echo mydomain.com | sed -r 's/.*\.([^.]+\.[^.]+)$/\1/'
mydomain.com
or as grail suggested, using awk:
Code:
$ echo 123.456.pippo.mydomain.com | awk -F. '{print $(NF-1) FS $NF}'
mydomain.com
Hope this helps.
 
1 members found this post helpful.
  


Reply



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
Using sed to match an IP address. mattst Linux - General 6 10-15-2012 06:14 PM
[SOLVED] How can I use sed to match this? ted_chou12 Linux - Newbie 13 12-09-2011 03:10 AM
Using sed regex to remove the top level domain from a fully qualified domain name linux2man Programming 6 12-01-2011 03:38 PM
sed match ted_chou12 Programming 7 04-20-2011 03:49 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM

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

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