Problem with scripting task

Discussion in 'Programming/Scripts' started by Poliman, Aug 27, 2018.

  1. Poliman

    Poliman Member

    I have a task to do in bash. There is a file:
    Code:
    [email protected]:~# 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.
     
  2. Poliman

    Poliman Member

    Does anybody know how to resolve this?
     
  3. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    One way to resolve it is to not do it in bash. Your algorithm is somewhat vague. The program needs to
    1. read the lines
    2. separate the IP-number and data parts
    3. Deal with the separator not being there
    4. Store them in a data structure that can be sorted and where the parts are available
    5. 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.
     
    Poliman likes this.
  4. Poliman

    Poliman Member

    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
     
    Last edited: Aug 28, 2018
  5. Poliman

    Poliman Member

    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
     
  6. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    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
     
  7. Poliman

    Poliman Member

    Not yet. Currently too hard. ;) Each zero in last octet has to be removed before sum.
     

Share This Page