LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-30-2011, 08:59 PM   #1
linux2man
Member
 
Registered: Oct 2006
Posts: 33

Rep: Reputation: 16
Using sed regex to remove the top level domain from a fully qualified domain name


Hi,
I have file contain 100 FQDN I want to remove TLD ".com, .org , .net ...." using sed regex not others, and redirect output to new file.

Thanks
 
Old 11-30-2011, 09:18 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
Code:
echo $line | sed -e 's/\.[a-z]*$//'
.. might need a tidy up but it should get you started
 
1 members found this post helpful.
Old 12-01-2011, 12:53 AM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Code:
test$ cat file
www.yahoo.com
www.msn.com
www.youtube.com
www.linuxquestions.org
www.google.com
www.kubuntuforums.net
www.amazon.com
test$ sed 's/\.[^.][^.]*$//' file
www.yahoo
www.msn
www.youtube
www.linuxquestions
www.google
www.kubuntuforums
www.amazon
test$ # TLD's can include characters outside [a-z]
test$ # http://en.wikipedia.org/wiki/Top_Level_Domain
 
Old 12-01-2011, 10:37 AM   #4
linux2man
Member
 
Registered: Oct 2006
Posts: 33

Original Poster
Rep: Reputation: 16
Thanks a lot it's working like sharm
Can you please describe the command
 
Old 12-01-2011, 12:39 PM   #5
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by linux2man View Post
Can you please describe the command
Code:
sed 's/\.[^.][^.]*$//' file
general format of command:
sed 'program' input-file

program enclosed in single quotes to prevent expansion by shell.

format of program:
s/regular-expression/replacement-string/
  • s = substitute command
  • \.[^.][^.]*$ = regular expression
  • replacement string is empty, so matched text is removed from pattern space

more information:

Code:
man sed
if using GNU sed then also see:

Code:
info sed
or http://www.gnu.org/software/sed/manu...ode/index.html


HTH

Last edited by Telengard; 12-01-2011 at 12:51 PM. Reason: change formatting for clarity
 
Old 12-01-2011, 04:27 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,758

Rep: Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644Reputation: 644
Code:
TLD's can include characters outside [a-z]
.. thats what I meant by cleanup
 
Old 12-01-2011, 04:38 PM   #7
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by kbp View Post
.. thats what I meant by cleanup
TBH I was surprised too when skimming the article.
 
  


Reply

Tags
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
[SOLVED] servers fully qualified domain name garyg007 Debian 6 07-23-2010 10:08 AM
Configuring Sendmail without a Fully Qualified Domain Name jroyce Linux - Software 2 04-18-2008 10:04 PM
problem with fully qualified domain name and apache nephish Linux - Server 2 04-09-2008 01:26 AM
Fully Qualified Domain Name tumana Linux - Networking 4 06-10-2004 03:58 AM
Fully Qualified Domain Names? MasterC Linux - Networking 7 05-19-2003 02:38 PM


All times are GMT -5. The time now is 11:15 AM.

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