Trying to combine upload and download totals from txt file by ip address

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

192.168.0.147 1565369
192.168.0.13 1664855
192.168.0.6 1332868

Downloads.txt

192.168.0.147 9838820
192.168.0.18 12051718
192.168.0.6 7633159

I want something that looks like

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!

Try this:

awk '{a[$1]+=$2}END{for(i in a)print i, a}' uploads.txt downloads.txt
1 Like

Thank you so much! It worked beautifully!

---------- Post updated at 04:34 PM ---------- Previous update was at 04:31 PM ----------

any chance that this or something like it would work from streams? IE:

iptables -nvx -L FORWARD | awk '$8 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/{print $8,$2}

How does the output look like and what is the desired output?

alternative way :wink:

# sed 's/ .*//' Uploads.txt Downloads.txt  | sort -t"." -u -k4,7rn | sed 's/\(.*\)/\1 9999999999/'
192.168.0.147 9999999999
192.168.0.18 9999999999
192.168.0.13 9999999999
192.168.0.6 9999999999

Have you read the question carefully?

The last column must be the sum of the downloads and uploads.

iptables -nvx -L FORWARD |awk '$8 ~ /[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/ {a[$8]+=$2}END{for(i in a)print i, a}'

ok i miss this :slight_smile:
i correct now :b:

# sed "" Uploads.txt Downloads.txt | sort -t"." -k4,7rn | sed 'N;/\(.*\) .*\n\1 .*$/s/\(.*\) \(.*\)\n\(.*\) \(.*\)/\1 \2+\4/;s/ \(.*+.*\)$/ \1/' |
> while read -r f s ; do echo $f `echo "$s"|bc`; done
192.168.0.147 11404189
192.168.0.18 12051718
192.168.0.13 1664855
192.168.0.6 8966027