add second field of two files on different servers
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
add second field of two files on different servers
Hi
I have one file placed at one server whose sample as follows:
57272 28
56767 0
57575 4
58888 6
53030 26
54242 0
56060 0
And another file at different server whose sample data as follows:
57272 22
56767 0
57575 1
58888 2
53030 13
54242 0
56060 1
Now i want to run a script from one of the server which adds second field from both files which are not on same server and gives the output as follows:
57272 50
56767 0
57575 5
58888 8
53030 39
54242 0
56060 1
Click here to see the post LQ members have rated as the most helpful post in this thread.
Let's say you can connect on both server using ssh and using public/private keys.
Let's say also that :
File 1 is /app1/f-one on server1.foo.org
File 2 is /app2/f-two on server2.foo.org
Then first you could create a script doit.sh :
Code:
#!/bin/bash
declare -a akey
declare -a aval
idx=0
while read k v; do
[ -z ${aval[$k]} ] && akey[$((idx++))]=$k
aval[$k]=$((${aval[$k]:-0}+$v))
done
i=0
while [ $i -lt $idx ]; do
echo "${akey[$i]} ${aval[${akey[$((i++))]}]}"
done
It's obvious that you will not forget to chmod +x doit.sh, then all you have to do is :
@Chirel: One tends to use the tools one knows. The longer you work with unix/linux the more tools you come across (and sometimes forget again...).
Your solution has one advantage over mine: It also works when the infiles are not the same.....
A simple sort might take care of that problem in my script, but that might not be what the OP wants.
add second field of two files on different servers
Hi,
@drunna thanks for your post, i want to tell you the file structure is same but count will be different.
But i want to do it without copying the file from both servers.
Quote:
Originally Posted by druuna
Hi,
@Chirel: One tends to use the tools one knows. The longer you work with unix/linux the more tools you come across (and sometimes forget again...).
Your solution has one advantage over mine: It also works when the infiles are not the same.....
A simple sort might take care of that problem in my script, but that might not be what the OP wants.
add second field of two files on different servers
Hi,
@drunna: Yes the first column will be same in both files of different servers that is same number in same location and below strategy which u mention according to my case not possible.
Quote:
Originally Posted by druuna
Hi,
There's not enough info to help.....
Do these 2 files always have the same first columns (same number in the same location) or is the following also possible???
my first post does have the sample command with the dummy inputs. if yours is not working i suspect your server isnt mounted in that directory (notice that my original post said: assuming nfs or sshfs). you can test that by posting the result of: ls /mnt/sshfs-10.0.0.1/home/smehan/applog/cp_report.txt /mnt/sshfs-10.0.0.2/home/smehan/applog/cp_report.txt
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.