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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Files

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
0
0
GB
Hello Guys,

I'm hoping this is the right room, i'm using Shell scripts so i hope i'm on the right track. I've looked at the DIFF and CMP commands but i'm struggling to put them to good use in this occasion.

I need to take two files which are a list of network address's and compare them, and then output a list of address that are unique to file 1.

So, let me try and draw you a clearer picture, i have two .txt files.

File 1
Code:
00:09:2D:31:69:4C-2
00:0E:6D:04:6D:06-9
00:0E:6D:E4:62:03-9

File 2
Code:
00:0E:6D:04:6D:06-9
00:0E:6D:E4:62:03-9

I would then need this comparison command to output 00:09:2D:31:69:4C-2 as that is the address unique to file 1.

The address's in both files are generated dynamicly so could be in any order and there could be any number of address's, the only thing i can gaurentee is that each address will only appear once in each file.

I've used the COMM command to try and do this but i seems to get itself confused if the order changes in each file, as if its doing a line by line comparison.

Any ideas? I'm looking to put this in a bash shell script,

Thanks in advance for any help you guys can pass my way.

Rob
 
Can you post the code you've already tried.

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Sure thing bud,

I've tried quite a few different approaches.

First one I tried was to combine the two files together, sort them, and remove any duplicates. This almost achieves the effect but obviously returns all the unique files in iether file 1 or 2 which is not what i want, i only need those from file 1. To achieve that i used this code.

Code:
cat /hj/bluepod/minrange /tmp/done > /hj/bluepod/macsinrange | sort /hj/bluepod/macsinrange | uniq -u

The next approach I took was to use the COMM command to look at the files and return all the results unique to file 1, which at first i thought was working, but i guess that as it does a line by line comparison the files may be in different orders and this can cause issues. For this i used the following command.

Code:
comm -2 -3 /hj/bluepod/minrange /tmp/done

I've tried a couple of other attempts to which i no longer have the code, but havnt managed to achieve anything as yet.

Thanks,

Rob
 
Just sort the files before using the comm command perhaps?

Annihilannic.
 
It's ok guys,

I've managed to solve the issue my own little way.

I stuck with the cat, sort and uniq method, i just doubled up the entries in the second file so the only time you will ever get a unique address on the system is in file one.

It works just how i want it and its very quick.

Thanks for the advice,

Rob
 
Hi Rob,

I have similar needs to compare two files, Could you pls post your solution(s).

Thanks
David
 
Hello David,

I ended up using the CAT function that is discussed above.

Basicly the process combines my two lists of address's and then sorts them into order which combines all repeated address's together, the UNIQ function then strips out everything that isnt unique, leaving you with only uniq address's.

The problem i had initialy was that it then provides me a list of address's that were unique to iether file 1 or file 2, when i only need those from file 1.

So to solve this isssue i duplicate all of the input into my file 2, so it looks like this.

Code:
00:0E:6D:04:6D:06-9
00:0E:6D:04:6D:06-9
00:0E:6D:E4:62:03-9
00:0E:6D:E4:62:03-9

And that way when i do my merge, and strip out everything uniq i only get uniq address's from file 1.

Thanks,

Rob
 
Code:
ruby -e 'puts gets(nil).to_a - gets(nil).to_a' file1 file2
 
Thanks for that Futurelet,

I've now changed the way my application runs so i'm not comparing three files, so my conbination method seems to work quite well for the moment.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top