Currently I'm facing problem with comparing two huges files on a particular key column. One file consists of 10k records and other one 18million records. Both files are | (pipe) delimited. I am comparing based on the first column in the two files and redirecting them to two separate files. If Key columns are same it has to pick the record from 10k records file and send it to one file. If the Key columns are not matching ie., the key column is present in 18 million records file but not in 10k records file, it has to go into another file.
Here I'm pasting the query what I have written, taking more time.
Currently I'm facing problem with comparing two huges files on a particular key column. One file consists of 10k records and other one 18million records. Both files are | (pipe) delimited. I am comparing based on the first column in the two files and redirecting them to two separate files. If Key columns are same it has to pick the record from 10k records file and send it to one file. If the Key columns are not matching ie., the key column is present in 18 million records file but not in 10k records file, it has to go into another file.#!/usr/bin/perl $oldFile1 = $ARGV[0]; $newFile1 = $ARGV[1]; $oldFile2 = "unchanged.txt"; $changes1 = "changed.txt"; if (!$oldFile1) { printf("Error: No old file is provided\n"); exit(1); } if (!$newFile1) { printf("Error: No new file is provided\n"); exit(1); } open(OLDFILE1, $oldFile1) or die("Can't open $oldFile1 for input: $!") +; open(NEWFILE1, $newFile1) or die("Can't open $newFile1 for input: $!") +; open(OLDFILE2, ">$oldFile2") or die("Can't open $oldFile2 for output: +$!"); open(CHANGES1, ">$changes1") or die("Can't open $changes1 for output: +$!"); foreach (<OLDFILE1>) { $line1 = $_; chomp $line1; ($x,$y,$z,$a,$b,$c)=split(/\|/,$line1); foreach (<NEWFILE1>) { $line2 = $_; chomp $line2; ($x1,$y1,$z1,$a1,$b1,$c1)=split(/\|/,$line2); if ($x eq $x1) { print OLDFILE2 "$line1\n"; } else { print CHANGES1 "$line2\n"; } } } close(OLDFILE1); close(NEWFILE1); close(OLDFILE2); close(CHANGES1);Hello,
Here I'm pasting the query what I have written, taking more time.
#!/usr/bin/perl $oldFile1 = $ARGV[0]; $newFile1 = $ARGV[1]; $oldFile2 = "unchanged.txt"; $changes1 = "changed.txt"; if (!$oldFile1) { printf("Error: No old file is provided\n"); exit(1); } if (!$newFile1) { printf("Error: No new file is provided\n"); exit(1); } open(OLDFILE1, $oldFile1) or die("Can't open $oldFile1 for input: $!") +; open(NEWFILE1, $newFile1) or die("Can't open $newFile1 for input: $!") +; open(OLDFILE2, ">$oldFile2") or die("Can't open $oldFile2 for output: +$!"); open(CHANGES1, ">$changes1") or die("Can't open $changes1 for output: +$!"); foreach (<OLDFILE1>) { $line1 = $_; chomp $line1; ($x,$y,$z,$a,$b,$c)=split(/\|/,$line1); foreach (<NEWFILE1>) { $line2 = $_; chomp $line2; ($x1,$y1,$z1,$a1,$b1,$c1)=split(/\|/,$line2); if ($x eq $x1) { print OLDFILE2 "$line1\n"; } else { print CHANGES1 "$line2\n"; } } } close(OLDFILE1); close(NEWFILE1); close(OLDFILE2); close(CHANGES1);
In reply to comparing two huges files by vamsikrishna
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |