nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

Shell Script To Calculate IP Addresses in a text file

This is a discussion on Shell Script To Calculate IP Addresses in a text file within the Shell scripting forums, part of the Development/Scripting category; Hi all, I just want to write a script which should do the following I am having a .txt file ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 28-08-2009, 01:01 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 451
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default Shell Script To Calculate IP Addresses in a text file

Hi all,

I just want to write a script which should do the following

I am having a .txt file which contains only 1 ip add/line as follows

123.176.40.60
123.176.40.60
190.196.23.164
190.196.23.164
190.196.23.164
190.196.23.164
60.220.224.103
60.220.224.103
60.220.224.103
60.220.224.103


Here my requirement is i have to analysis this file and calculate some stats as follows..

How many times an ip add repeated.. and what is the ip add..
suppose i have a ip add in that file 60.220.224.103 my out put should say
the ip add 60.220.224.103 repeated 3 times in the file
thats it..
i am new to sed/awk scripting tried it but unable to get the solution..


Update:I got this command when I searched google.. grep -c "124.238.251.196" root-d-ip.txt
This command will give you the no of occurences of 124.238.251.196 in root-d-ip.txt ...
but how to get the variable from a file ie..
i want to do as follows
grep -c "$ver1" root-d-ip.txt
bcz my file is nearly 100MB size i cant go to each and every line and get the ip add as variable .. so is there any way i can store this ip add in to some kind of variable so that i can keep that one for -c option for grep ????

Last edited by kumarat9pm; 28-08-2009 at 05:54 PM.
Reply With Quote
  #2 (permalink)  
Old 28-08-2009, 06:07 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

PHP Code:
#!/bin/bash
IP=$1
INPUT
=/path/toroot-d-ip.txt 
[ $# -eq 0 ] && { echo "Usage: $0 ip-address"; exit 999; }
grep -"${IP}" ${INPUT
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #3 (permalink)  
Old 28-08-2009, 08:11 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 451
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default I want shell script only

Hi Nixcraft,

As i am not aware of PHP scripts i just copied and pasted the code but not at all useful

here is what i executed and output i got

#!/bin/bash
IP=$1
INPUT=/root/Script/root-d-ip.txt
[ $# -eq 0 ] && { echo "Usage: $0 ip-address"; exit 999; }
grep -c "${IP}" ${INPUT}

Output:
Usage: test.sh ip-address


thats it .. i dont know where i did mistake and i didnt understand the code which you given, if possible can you explain.
Reply With Quote
  #4 (permalink)  
Old 28-08-2009, 08:27 PM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

If first line is
  1. #!/bin/bash - shell script
  2. #!/usr/bin/perl - perl script
  3. #!/usr/bin/python - python script
  4. #!/usr/bin/php - PHP script

Run it as
Code:
test.sh 60.220.224.103
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #5 (permalink)  
Old 28-08-2009, 08:46 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 451
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default I think, i am not clear from my end let me explain my problem

Actually i am having one file which contains only one ip add/line
as below
123.176.40.60
123.176.40.60
190.196.23.164
190.196.23.164
190.196.23.164
190.196.23.164
60.220.224.103
60.220.224.103
60.220.224.103
60.220.224.103

so my script should do as follows
it has to see line by line for ip add, in first line it found new ip(123.176.40.60) so this value should be moved to a variable say v1 and second thrid line it found one more new ip add so this ip add should be stored in v2 and so on.. when the script found again v1(123.176.40.60) it should increment the count for that ip(ie the count should be 2 now for the v1 occurrence in that file) then calculate how many times that ip is repeated...
and once it calculate that ip after it counts all the ip .. it has to give the statistics as follos
the ipadd $v1 is repeated: $nooftimesv1repeated
the ipadd $v2 is repeated: $nooftimesv2repeated so on...
Please let me if you requre any other info.
Reply With Quote
  #6 (permalink)  
Old 29-08-2009, 12:14 AM
nixcraft's Avatar
Never say die
User
 
Join Date: Jan 2005
Location: BIOS
OS: RHEL
Scripting language: Bash and Python
Posts: 2,710
Thanks: 11
Thanked 245 Times in 184 Posts
Rep Power: 10
nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute nixcraft has a reputation beyond repute
Default

Try
Code:
#!/bin/bash
IP=$1
INPUT=/path/toroot-d-ip.txt 
[ $# -eq 0 ] && { echo "Usage: $0 ip-address"; exit 999; }
COUNT=$(grep -c "${IP}" ${INPUT})
echo "the ipadd $IP is repeated: $COUNT times"
__________________
Vivek Gite
Linux Evangelist
Be proud RHEL user, and let the world know about your enterprise choices! Join RedHat user group.
Always use CODE tags for posting system output and commands!
Do you run a Linux? Let's face it, you need help
Reply With Quote
  #7 (permalink)  
Old 29-08-2009, 04:56 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 87
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default

Code:
sort "$INPUT" | uniq -c
Reply With Quote
The Following User Says Thank You to cfajohnson For This Useful Post:
kumarat9pm (29-08-2009)
  #8 (permalink)  
Old 29-08-2009, 02:00 PM
kumarat9pm's Avatar
Senior Member
User
 
Join Date: Jun 2007
Location: Pune,MH,India
OS: RHEL,UBUNTU..
Posts: 451
Thanks: 20
Thanked 20 Times in 18 Posts
Rep Power: 5
kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about kumarat9pm has a spectacular aura about
Send a message via Yahoo to kumarat9pm Send a message via Skype™ to kumarat9pm
Default thats really a uniq command man

Really its good command thanks for sharing man
this solved the problem
Reply With Quote
Reply

Tags
grep , grep count , ip addresses , shell scripting , text file


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
[Solved] Alphabetize text within a file jaysunn Shell scripting 8 02-07-2009 01:45 PM
Shell Script To change strings / text in a text file jaysunn Shell scripting 1 08-05-2009 05:58 PM
How to Redirect OutPut of Shell Command to a text file Hadi Shell scripting 1 29-04-2009 07:27 PM
Shell Script To Outputs File Permissions of Most Recently Modified File glen_4455 Shell scripting 1 25-08-2008 02:39 PM
shell script to search specific file from txt file inside zip file and extract it aasif.shaikh Shell scripting 2 31-05-2008 06:44 PM


All times are GMT +5.5. The time now is 03:11 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38