I have a task to do in bash. There is a file: Code: root@serwer1:~# cat lista_ip.log 192.168.100.1-12345 192.168.100.1-3342 192.168.100.2-341245 192.168.100.2-345 192.168.100.1-8912 192.168.100.3-156 192.168.100.5-93454 192.168.100.4-2546 192.168.100.4-972335 which contains ip addresses related with some data send somewhere. I need to sum up data being sent for each ip address. My idea is - in theory: 1. Put it into the table. 2. Sort this table based on ip address. 3. Add values of data send together until detect another ip. PS Data in file can be with "-" or without separator.
One way to resolve it is to not do it in bash. Your algorithm is somewhat vague. The program needs to read the lines separate the IP-number and data parts Deal with the separator not being there Store them in a data structure that can be sorted and where the parts are available iterate that structure adding the values per IP-address and printing them out. I would not do this kind of script in bash, too much difficult debugging. I remember you previously said you wanted to learn bash, do that and try writing it.
I think that here could help using awk and maybe grep. I have to find out how provide the form, from this: 192.168.100.1-12345 192.168.100.1-3342 to this: 192.168.100.1 12345 192.168.100.1 3342 After this I could put it to array and sort it. Last step would be an interation where I could try adding values until loop recognize that current ip is different and then I could print the summarized values for specific IP. PS Currently I did (yes, I know, it's almost nothing): Code: sort -n lista_ip.log > sorted.log ip=($(awk 'BEGIN {FS="-"} {print $1 $2}' /root/sorted.log)) function search { for found_ip in $ip do if [ $found_ip= ] #adding and printing the values fi done } search $ip
I resolved it. Here is the result: Code: sort -n lista_ip.log > sorted.log awk -F '-' '{a[$1] += $2} END{for (i in a) print i, a[i]}' sorted.log
Does the sorting work if you have leading zeros in the IP-numbers? Code: 192.168.100.1-12345 192.168.100.01-3342 192.168.100.92-341245 192.168.100.093-345 192.168.100.002-8912