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

  • Home
  • SEARCH
  • 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 88809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:37:05+00:00 2026-05-10T22:37:05+00:00

Suppose you have a file that contains IP addresses, one address in each line:

  • 0

Suppose you have a file that contains IP addresses, one address in each line:

10.0.10.1 10.0.10.1 10.0.10.3 10.0.10.2 10.0.10.1 

You need a shell script that counts for each IP address how many times it appears in the file. For the previous input you need the following output:

10.0.10.1 3 10.0.10.2 1 10.0.10.3 1 

One way to do this is:

cat ip_addresses |uniq |while read ip do     echo -n $ip' '     grep -c $ip ip_addresses done 

However it is really far from being efficient.

How would you solve this problem more efficiently using bash?

(One thing to add: I know it can be solved from perl or awk, I’m interested in a better solution in bash, not in those languages.)

ADDITIONAL INFO:

Suppose that the source file is 5GB and the machine running the algorithm has 4GB. So sort is not an efficient solution, neither is reading the file more than once.

I liked the hashtable-like solution – anybody can provide improvements to that solution?

ADDITIONAL INFO #2:

Some people asked why would I bother doing it in bash when it is way easier in e.g. perl. The reason is that on the machine I had to do this perl wasn’t available for me. It was a custom built linux machine without most of the tools I’m used to. And I think it was an interesting problem.

So please, don’t blame the question, just ignore it if you don’t like it. 🙂

  • 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. 2026-05-10T22:37:06+00:00Added an answer on May 10, 2026 at 10:37 pm
    sort ip_addresses | uniq -c 

    This will print the count first, but other than that it should be exactly what you want.

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

Sidebar

Ask A Question

Stats

  • Questions 66k
  • Answers 66k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Nothing specific to FlexBuilder. If you need such a functionality,… May 11, 2026 at 11:28 am
  • added an answer I don't think it can be done without tracing the… May 11, 2026 at 11:28 am
  • added an answer The 'schemas.xmlsoap.org' namespaces are for SOAP 1.1. See the W3C… May 11, 2026 at 11:28 am

Related Questions

Suppose you have a file that contains IP addresses, one address in each line:
Suppose you have a large file made up of a bunch of fixed size
Suppose you have a full path to an accessible file or folder on the
Suppose you have a collection of a few hundred in-memory objects and you need
Suppose you have a collection of Foo classes: class Foo { public string Bar;
Suppose you have a method with some pre and post-conditions. Is it ok to
Suppose you have the tables Presentations and Events . When a presentation is saved
Suppose you have two models, User and City, joined by a third model CityPermission:
Why is using '*' to build a view bad ? Suppose that you have
It seems that everybody knows you're supposed to have a clear distinction between the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.