Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remote file comparisions

Status
Not open for further replies.

pingjocky

Programmer
Dec 9, 2002
1
US
I need to compare two files, on different machines. I download (copy) a file from the remote machine, then need to compare the one on my machine with the one on the remote box. If the files are different, then the script will need to download the file again and compare the two again.
Any ideas on how to do this. I have a script that sort of does it but I am getting errors with it...sometimes and it is pretty long.

pingjocky
 
That's the long way home.
Netcat is your friend for little things like this.

A more meticulous approach that uses gawk 3.1.0's
network capability is something like this:
##
function openFile(fname, arra,i) {
i=1
while ((getline < fname) > 0) {
mylist = length(mylist) < 1 ? $0&quot;\n&quot; : mylist&quot;\n&quot;$0&quot;\n&quot;
i++;
}
printf &quot;Read %d records\n&quot;, i
close(fname)
return mylist
}


function compFiles(arr1,arr2,len, x) {
for (x=0 ; x <= len ; x++) {
printf &quot;Comparing %s and %s\n&quot;, arr1[x], arr2[x]
if (arr1[x] != arr2[x]) {
return 0
}
}
return 1
}

function copytoFile(farray,xx, i) {
i=0
printf &quot;Filename(full path) to copy data to: &quot;
getline name < &quot;-&quot;
while (i <= xx) {
print farray >> name
i++
}
close(name)
return i
}

BEGIN {
port = 7700
if (ARGV[1] == &quot;client&quot; && ARGC == 3) {
printf &quot;Address of server: &quot;
getline add < &quot;-&quot;
plist = openFile(ARGV[2])
z = split(plist,clientfile,&quot;\n&quot;)
cnt = 1
sock = &quot;/inet/tcp/0/&quot; add &quot;/&quot; port
printf &quot;\r\n&quot; |& sock
while ((sock |& getline) > 0) {
serverfile[cnt++] = $0
}
close(sock)
printf &quot;Local file length = %d\n, remote file length = %d\n&quot;, z, cnt
if (z == cnt && (x = compFiles(clientfile,serverfile,z)) > 0) {
printf &quot;Files are the same\n&quot;
exit
} else {
printf &quot;Files are not the same\n&quot;
copytoFile(serverfile,cnt)
exit
}
} else if (ARGV[1] == &quot;server&quot; && ARGC == 3) {
flg = 1
plist = openFile(ARGV[2])
z = split(plist,serverfile,&quot;\n&quot;)
service = &quot;/inet/tcp/&quot; port &quot;/0/0&quot;
#feed the socket data
do {
fcnt = 0
#wait for the connect
while ((service |& getline) > 0 && fcnt++ <= z) {
printf plist |& service
print &quot;Sending&quot;, plist
close(service)
}
#close the connection
} while (flg == 1)
} else {
printf &quot;Improper args to script %s, %d\n&quot;, ARGV[1], ARGC
exit
}
}

Poor mans awk mmap and file server in 80 lines ;)
Called like this for server:
gawk -f scriptname server &quot;filename1&quot;
gawk -f scriptname client &quot;filename2&quot;

You should be able to adapt the idea easily enough.
HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top