LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sed regex to remove the top level domain from a fully qualified domain name (https://www.linuxquestions.org/questions/programming-9/using-sed-regex-to-remove-the-top-level-domain-from-a-fully-qualified-domain-name-916473/)

linux2man 11-30-2011 07:59 PM

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

kbp 11-30-2011 08:18 PM

Code:

echo $line | sed -e 's/\.[a-z]*$//'
.. might need a tidy up but it should get you started

Telengard 11-30-2011 11:53 PM

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


linux2man 12-01-2011 09:37 AM

Thanks a lot it's working like sharm
Can you please describe the command

Telengard 12-01-2011 11:39 AM

Quote:

Originally Posted by linux2man (Post 4539175)
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

kbp 12-01-2011 03:27 PM

Code:

TLD's can include characters outside [a-z]
.. thats what I meant by cleanup :)

Telengard 12-01-2011 03:38 PM

Quote:

Originally Posted by kbp (Post 4539492)
.. thats what I meant by cleanup :)

TBH I was surprised too when skimming the article. :p


All times are GMT -5. The time now is 09:19 AM.