Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:14:18+00:00 2026-06-08T07:14:18+00:00

Okay so what I am trying to figure out is how do I count

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:14:19+00:00Added an answer on June 8, 2026 at 7:14 am

    If you don’t need to count the dots, but just remove the penultimate dot and everything afterwards, you can use Bash’s built-in string manuipulation.

    ${string%substring}
    Deletes shortest match of $substring from back of $string.

    Example:

    $ string="aaa.bbb.ccc.ddd.google.com"
    $ echo ${string%.*.*} 
    aaa.bbb.ccc.ddd
    

    Nice and simple and no need for sed, awk or cut!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I been trying to work this out but unable too. I have a
Okay, I just spent the last four hours trying to figure out why my
I am trying to figure out a format spec of to_char() that would give
Okay I've been trying to figure this out for a while now. I have
Okay, I've been banging my head trying to figure out how to get around
I'm trying to figure out a way that I can send messages to other
Okay so I'm trying to make a little gag program that will run away
Okay here is what I'm trying to do: I have a model object that
I'm trying to figure out the best approach to take in my current situation.
Odd problem, trying to figure out what is going on here with my fresh

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.