Count the number of occurrences in a string. Linux
===

Okay so what I am trying to figure out is how do I count the number of periods in a string and then cut everything up to that point but minus 2. Meaning like this:

    string="aaa.bbb.ccc.ddd.google.com"
    
    number_of_periods="5"
    
    number_of_periods=`expr $number_of_periods-2`
    
    string=`echo $string | cut -d"." -f$number_of_periods`
    
    echo $string
    
    result: "aaa.bbb.ccc.ddd"

The way that I was thinking of doing it was sending the string to a text file and then just greping for the number of times like this:

     grep -c "." infile
The reason I don't want to do that is because I want to avoid creating another text file for I do not have permission to do so. It would also be simpler for the code I am trying to build right now.

**EDIT**

I don't think I made it clear but I want to make finding the number of periods more dynamic because the address I will be looking at will change as the script moves forward.