Filtering two files with uncommon column

Reedick, Andrew jr9445 at ATT.COM
Fri Jan 18 13:25:55 EST 2008


> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of Madhur
> Sent: Friday, January 18, 2008 4:23 AM
> To: python-list at python.org
> Subject: Filtering two files with uncommon column
> 
> 
> Basically I want to compare the two files based on second column. If
> the second
> column matches on both the files do not print anything, else if there
> is no matc
> h in for the second column for first file in second file then print it
> under Fil
> e1 header, else if there is no match for the second column for second
> file in fi
> rst file print it under File2 header.
> 


I often do this to compare property files between environments.  The
follow algorithm works for any number of files by creating a dictionary
of lists (or hash of arrays in Perl-ese.)

Create a dictionary
Index = -1
For file in files
	Index++
	For line in file
		col = match/split/regex the column
		If col not in dictionary
			Dictionary[col] = []

		extend dictionary[col] to length of index
		dictionary[col][index] = col

for col in sort(dictionary.keys()):
	extend dictionary[col] to length of index
	print dictionary[col]	




*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA622





More information about the Python-list mailing list