LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-30-2011, 07:59 PM   #1
linux2man
Member
 
Registered: Oct 2006
Posts: 34

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, 08:18 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
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 11-30-2011, 11:53 PM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 148Reputation: 148
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, 09:37 AM   #4
linux2man
Member
 
Registered: Oct 2006
Posts: 34

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

Rep: Reputation: 148Reputation: 148
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 11:51 AM. Reason: change formatting for clarity
 
Old 12-01-2011, 03:27 PM   #6
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

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

Rep: Reputation: 148Reputation: 148
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



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

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

All times are GMT -5. The time now is 01:46 AM.

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