Parse string

Hi, I need to parse a string, check if there are periods and strip the string.
For example i have the following domains and subdomains: mydomain.com, dev.mydomain.com

I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain.

I use this for domains but i don't know how to do it for subdomains also

dom=(`echo $i | tr '.' ' '`)

Thank you

With awk:

echo "dev.mydomain.com" | awk -F"." '{print $(NF-1)}'
dom=${i//.}

Unfortunately it doesn't work because I don't know if will be a domain or a subdomain. If the output is:

dev.mydomain.com

I need to parse each string and strip the domain and periods.It works with tr but the problem is how do i check if it's a subdomain.For the two strings above it should result:

mydomain
devmydomain

Yes it does work. Did you try?

$ echo "dev.mydomain.com" | awk -F"." '{print $(NF-1)}'
mydomain
$ echo "mydomain.com" | awk -F"." '{print $(NF-1)}'
mydomain

Oops misread you requirements...
I guess you would need two statements

j=${i/.com}
dom=${j//.}

or

dom=$(echo $i|sed -e's/\.com//;s/\.//g')

Thank you, it works but for different domain extensions like .net, .org ..ow do I do it ? I don't know what domain extension would be, it could be .net or aomething else

$ dom="mydomain.com"; echo ${dom%.*} | sed 's/\.//g'
mydomain

$ dom="dev.mydomain.com"; echo ${dom%.*} | sed 's/\.//g'
devmydomain

Thank you but I think you misunderstood, without the domain extension, without .com, .net, .org or other domain extension. it must be something generic because I don't know the domain extension.
The examples above should be:
devmydomain (without com)
mydomain (without com)

> cat infile
dev.mydomain.com
mydomain.com
usca.berkeley.edu
perl -ne '@_ = split /\./;for ($i=0;$i<scalar(@_)-1;$i++) {print @_[$i]};print "\n"' infile
> perl -ne '@_ = split /\./;for ($i=0;$i<scalar(@_)-1;$i++) {print @_[$i]};print "\n"' infile
devmydomain
mydomain
uscaberkeley

Well,

j=${i%.*}
dom=${j//.}

or

dom=$(echo $i|sed -e's/\.[^.]*$//;s/\.//g')

or

dom=$(echo ${i%.*}|sed -e's/\.//g')

Would take care of that. But there are also various top level domains in two parts, e.g. .co.uk or .co.au. You would have to write exceptions for those cases if you want to cover everything.

Yes, I edited my post right after posting it, sorry :X
http://www.unix.com/shell-programming-scripting/118647-parse-string-2.html\#post302350975

Thank you all, it works now :slight_smile: .Problem solved, great forum and users