Results 1 to 8 of 8
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:
...
- 09-06-2010 #1Just Joined!
- Join Date
- Sep 2010
- Posts
- 2
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
Downloads.txtCode:192.168.0.147 1565369 192.168.0.13 1664855 192.168.0.6 1332868
I want something that looks likeCode:192.168.0.147 9838820 192.168.0.18 12051718 192.168.0.6 7633159
where 999999 is the sum of the downloads and uploads.Code:192.168.0.147 9999999999 192.168.0.18 9999999999 192.168.0.13 9999999999 192.168.0.6 9999999999
Thank you!
- 09-06-2010 #2Just Joined!
- Join Date
- Aug 2010
- Posts
- 18
Code:paste /tmp/u.txt /tmp/d.txt | while read ip u ip d ; do sum=$(($d+$u)); echo $ip $sum; done
- 09-06-2010 #3
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.
All without warranty. Haven't tested it.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.txtLast edited by Kloschüssel; 09-06-2010 at 11:46 AM.
- 09-06-2010 #4Just Joined!
- Join Date
- Aug 2010
- Posts
- 18
I assumed all lines are corresponding in both files and in the example it was a mistake
- 09-06-2010 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
This question is also answered here:
Trying to combine upload and download totals from txt file by ip address - The UNIX and Linux Forums
Regards
- 09-06-2010 #6Just Joined!
- Join Date
- Aug 2010
- Posts
- 18
Yes, nice solution.
- 09-06-2010 #7
How I hate double posts. *pf* Why noone uses the search function or web search engines?
- 09-06-2010 #8Just Joined!
- Join Date
- Sep 2010
- Posts
- 2


Reply With Quote
