Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Shell Scripting: Trying to combine upload and download totals from txt file by ip add


    I have two files, uploads.txt and downloads.txt. I would like to combine the columns of these files based on the ip address. How can I best do this?

    Uploads.txt
    Code:
    192.168.0.147 1565369
    192.168.0.13 1664855
    192.168.0.6 1332868
    Downloads.txt
    Code:
    192.168.0.147 9838820
    192.168.0.18 12051718
    192.168.0.6 7633159
    I want something that looks like
    Code:
    192.168.0.147 9999999999
    192.168.0.18 9999999999
    192.168.0.13 9999999999
    192.168.0.6 9999999999
    where 999999 is the sum of the downloads and uploads.

    Thank you!

  2. #2
    Code:
    paste /tmp/u.txt /tmp/d.txt | while read ip u ip d ; do sum=$(($d+$u)); echo $ip $sum; done

  3. #3
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    This won't work if the lines of each file do not correspond to the same IP. I.e. given the example, line 3 (ip 0.6 and 0.13) will be summed up but shouldn't.

    Code:
    #!/bin/sh
    
    while read dline
    do
      # sto
      ip=`sed 's/^(.*) [0-9]*$/\^1/g < $dline`
      total=`sed 's/^.*([0-9]*)$/\^1/g < $dline`
      while read uline
        do
          # add
          part=`sed 's/^.*([0-9]*)$/\^1/g < $uline`
          total=`expr $total + $part`
        done < (grep $d u.txt)
       # out
       echo $ip $total
    done < d.txt
    All without warranty. Haven't tested it.
    Last edited by Kloschüssel; 09-06-2010 at 11:46 AM.

  4. $spacer_open
    $spacer_close
  5. #4
    I assumed all lines are corresponding in both files and in the example it was a mistake

  6. #5

  7. #6
    Yes, nice solution.

  8. #7
    Linux Engineer Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    773
    How I hate double posts. *pf* Why noone uses the search function or web search engines?

  9. #8
    Quote Originally Posted by Kloschüssel View Post
    How I hate double posts. *pf* Why noone uses the search function or web search engines?
    If you will look closely, you will notice that I double posted on both sites in-case the member sets do not overlap.

    The best solution is:

    Code:
    awk '{a[$1]+=$2}END{for(i in a)print i, a[i]}' uploads.txt downloads.txt
    Thank you all for your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •