Linux / UNIX Tech Support Forum
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 ...
|
|||||||
| Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
| Sponsored Links | ||
|
|
|
||||
|
PHP Code:
__________________
Vivek Gite Linux Evangelist |
|
||||
|
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. |
|
||||
|
If first line is
Run it as Code:
test.sh 60.220.224.103
__________________
Vivek Gite Linux Evangelist |
|
||||
|
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. |
|
||||
|
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 |
|
|||
|
Code:
sort "$INPUT" | uniq -c |
| The Following User Says Thank You to cfajohnson For This Useful Post: | ||
kumarat9pm (29-08-2009)
| ||
![]() |
| 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 | |
|
|
|
||||
| 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 |