PERL Scripting: Diff 2 files and save output to file3

Hi,

I need to create a script to compare 2 files and store the output in a 3rd file.
This is how I do manually, but since I need to do this for about 150 files every week, I am trying to automate it using perl.
diff -u file1 file2 > file3.patch

For my script,

  • I have 2 files under different file paths, say
    $gpath = $WS_GOOGLE_COMPARE . $;
    $lpath = $WS_LOCAL . $
    ;

  • I want to diff these files in 2 different paths and store the output in a 3rd file path location, say /home/diff.patch (this is a third path location)

Please help.

Will the two folders have files with same name? So if I found /FolderA/XXX.txt, there will be a file name /FolderB/XXX.txt ?

yes, they will have the same file names.

(There are a few files that are present in folderA but not in folderB. In that case I need to copy them as is to folderB)

FolderA=/XXX/XXX
FolderB=/YYY/YYY
Difffolder=/ZZZ/ZZZ

cd $FolderA

for file in $(ls *)
do
   diff $FolderA/file $FolderB/file > $Difffolder/diff.patc
done

I need to the diff in perl. This wouldn't work.

I already have a file.txt with the list of filename. I open this file and for each file, I need to do the diff.

This is my code so far..

open(DIFF_FILE, "/folder/diff.txt") || die("Could not open file!");

  while($line = <DIFF_FILE>)
	  {
		push(@arrayOfdifffiles, $line);
		foreach $_ (@arrayOfdifffiles)
			{ 
			chomp($_);
                                      
			$gpath = $WS_GOOGLE_COMPARE . $_;
			$bpath = $WS_LOCAL . $_;
                                  #Need to diff the above files and save them to a 3rd file, until the end of "/folder/diff.txt" file  ? This is where I am getting stuck.

}
		}
		
close(DIFF_FILE);

Thanks again for your time !

---------- Post updated at 10:19 AM ---------- Previous update was at 10:09 AM ----------

This is what I need to do the following within the script (but not sure about the right syntax):

system("diff -u $gpath $bpath" >! '/folderX/diff.patch');

---------- Post updated at 06:10 PM ---------- Previous update was at 10:19 AM ----------

Thanks rdcwayx, for all your help.

I was able to figure it out.