Compare two csv files perl
Shell Programming and Scripting. Need Perl script to compare two CSV files. Registered User. Join Date: Sep Here you go. Code :. Join Date: Mar Join Date: Feb To write a script to test anybody would need a small set of data to test. The files you have are so huge. But any way try this. Last edited by Franklin52; at AM.. Reason: Please use code tags, thank you. Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche.
Example: I have files in below format file 1: zxc,,joe example. Script to compare count of two csv files. Hi Guys, I need to write a script to compare the count of two csv files each having 5 columns. Everyday a csv file is recived. Now we need to compare the count of todays csv file with yesterday's csv file and if the total count of records is same in todays csv file and yesterday csv file out Hi All, I've got in a situation where I need to convert.
Please find the script below. Expand Select Wrap Line Numbers. Well, of course it's slow. You're scanning through a large portion of file2 for every line in file1. This means that your your execute time is relative to the square of the size of the files. Ignoring your current algorithm for now though, I would suggest that you look into a cpan module to do this for you. I don't know what type of output this module will provide, but I'm almost certainly that it can be adapted in such a way to acheive the results you desire.
I think Text::Diff might be overkill if it's just a simple comparison of matching lines between the two files. Text::Diff also has the unfortunate behavior of slurping all files into memory, which may or may not be a problem. AdrianH 1, Expert 1GB. The easist way is to use something that is already made. Try using diff. It is a Unix utility and is designed for this sort of work. Of course it will not work if the records are not in the same order. In which case, you would have to go back to perl.
Adrian Rethinking this, if the key is at begining of the line, you could sort and then use diff. Why are you assuming unix? Looks like windows to me. There are GNU ports of Unix utilities all over the place. True enough filler for message too short.
Post Reply. Ok, I think I see. I have updated the issue with more pertinent code — perlstudent. Add a comment. Active Oldest Votes. So, with your code there - you've read in 'file1' to a hash. Improve this answer. Sobrique Sobrique Code snippet edited. I wonder how I can get it to match cells or if I can do that in this way. If I have a 2-columned CSV as file one and a 3-columned CSV as file two or a 1-column csv as file one and a 2-columned csv as file 2, it will tell me there was no match.
Also, if I switch the columns in file 1 given both csv's are 2-columned , but not file 2, it will tell me that there is no match. The quick and easy way: use split. If you're sure that doesn't happen in your data, the split will do the trick. Show 3 more comments. I think this code identifies every place that a data field in file A matches a data field in file B at least it does on my limited test data : use strict; use warnings; my arr1; my arr2; a.
Scooter Scooter 6, 8 8 gold badges 35 35 silver badges 60 60 bronze badges. This works too, but it only seems to match complete lines. If there are identical columns in the same order, it matches.
Add an extra column to the second file, and nothing. If I have one line 1,2,3,4,5 in the first file, and 1,2,3,4,5,6 in the second, I get 5 matches. Same if I have a,b,c,d,e,f in the first and a,b,c,d,e in the second. Can you provide your test data? You are using semi-colons as the delimiter instead of a comma.
0コメント