LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-29-2010, 11:09 AM   #1
xbaez
Member
 
Registered: Mar 2004
Location: USA
Distribution: Ubuntu
Posts: 291

Rep: Reputation: 30
Sort IP addresses for a bash program


Hello
I have for example the following IP addresses:

24.172.220.218
41.239.36.19
63.215.202.234
66.176.124.22
78.110.50.139
79.143.179.98
83.4.136.214
84.111.37.26
163.24.136.10
187.40.64.252
187.41.94.232
24.172.220.218
187.45.225.192
189.10.91.21
190.77.158.128
193.34.144.193

How can I sort those IP addresses?
I want to sort them using the first 3 numbers

I also want to count the number of times that address is repeated

This is a batch program

Thanks a lot
 
Old 04-29-2010, 11:22 AM   #2
djsmiley2k
Member
 
Registered: Feb 2005
Location: Coventry, UK
Distribution: Home: Gentoo x86/amd64, Debian ppc. Work: Ubuntu, SuSe, CentOS
Posts: 343
Blog Entries: 1

Rep: Reputation: 72
sort

sort -n should do it for you, and there is options within grep to count the number of times something appears (I believe its grep at least).

so

Code:
cat list of ips | sort -n > list_of_sorted_ips
grep list_of_sorted_ips (some option for count)
 
Old 04-29-2010, 12:17 PM   #3
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Quote:
Originally Posted by djsmiley2k View Post
... there is options within grep to count the number of times something appears...
There is - grep -c <pattern> - but this counts the number of times <pattern> appears (meaning that you'd have to repeat it for each IP address.

The command you want is uniq -c

Code:
[rh@mypc ~]$ cat ip_list | sort -n | uniq -c
      2 24.172.220.218
      1 41.239.36.19
      1 63.215.202.234
      1 66.176.124.22
      1 78.110.50.139
      1 79.143.179.98
      1 83.4.136.214
      1 84.111.37.26
      1 163.24.136.10
      1 187.40.64.252
      1 187.41.94.232
      1 187.45.225.192
      1 189.10.91.21
      1 190.77.158.128
      1 193.34.144.193
 
Old 04-30-2010, 01:08 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Code:
sort -n -t"." -k1,3 ips.txt | uniq -c
 
Old 05-14-2010, 10:13 AM   #5
cola
Senior Member
 
Registered: Sep 2007
Posts: 1,045

Rep: Reputation: 65
Quote:
Originally Posted by xbaez View Post
Hello
I have for example the following IP addresses:

24.172.220.218
41.239.36.19
63.215.202.234
66.176.124.22
78.110.50.139
79.143.179.98
83.4.136.214
84.111.37.26
163.24.136.10
187.40.64.252
187.41.94.232
24.172.220.218
187.45.225.192
189.10.91.21
190.77.158.128
193.34.144.193

How can I sort those IP addresses?
I want to sort them using the first 3 numbers

I also want to count the number of times that address is repeated

This is a batch program

Thanks a lot
You can mark the thread as "[SOLVED]".
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash sort problem ArthurHuang Programming 4 05-02-2009 05:20 PM
bash regex - sort czezz Programming 3 02-05-2009 07:41 PM
How to uninstall TMSNC same sort of program ServalSoft Linux - Newbie 7 09-30-2008 07:51 PM
please help with small sort program stuckatc Linux - Newbie 1 06-01-2006 01:17 PM
bash sort files by date in file name thedude2010 Programming 6 05-12-2006 11:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration