PERL Scripting: Diff 2 files and save output to file3


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL Scripting: Diff 2 files and save output to file3
# 1  
Old 07-20-2010
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.
# 2  
Old 07-20-2010
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 ?
# 3  
Old 07-20-2010
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)
# 4  
Old 07-20-2010
Code:
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

# 5  
Old 07-21-2010
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..


Code:
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):

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Save output into file bash scripting

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 ... (1 Reply)
Discussion started by: shdin271
1 Replies

2. Shell Programming and Scripting

Save output into file bash scripting

Hi there. i have created a program that in the end it will give output like this 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 .............. 17 i wonder how to save the output into a single string and into a file. i.e 1 10 11 12 9 2 3 8 13 14 7 4 5 6 15 17 (in this order,... (3 Replies)
Discussion started by: shdin271
3 Replies

3. Shell Programming and Scripting

Diff of 2 files using perl

Assitance needed in checking file difference using a perl script Below is the example I wanted a report saying file2 has an update of new value. File1 old date Alpha 156 Beta 255 Gama 6987 segma 4578 File2 new date Alpha 156 Beta 255 Gama 1000 (3 Replies)
Discussion started by: ajayram_arya
3 Replies

4. Shell Programming and Scripting

serach diff filename in diff location using shell scripting

Hi, I am new to shell scripting. please help me to find out the solution. I need a script where we need to read the text file(consists of all file names) and get the file names one by one and append the date suffix for each file name as 'yyyymmdd' . Then search each file if exists... (1 Reply)
Discussion started by: Lucky123
1 Replies

5. Shell Programming and Scripting

Perl - save results to output file.

Can any one please help, the code works...I want the output of $result to be saved in an output.txt file which is lcoated in c:\\temp\\output.txt. $filepath="C:\\temp\\ip.txt"; open (HOSTLIST,"$filepath"); @hosts=(<HOSTLIST>); foreach $host(@hosts) { $results = `nslookup... (1 Reply)
Discussion started by: sureshcisco
1 Replies

6. Shell Programming and Scripting

How to add data from 2 input files and save it in 1 output file

Hi, i have 2 input files which are file1.txt and file2.txt. I need to extract data from file1.txt and file2.txt and save it in file3.txt like example below:- File1.txt ID scrap1 Name scrap1 start 1 end 10 ID scrap2 Name scrap2 start 11 end ... (4 Replies)
Discussion started by: redse171
4 Replies

7. Shell Programming and Scripting

diff 2 files > file3, but records in various order

What I really need is a script that compares 2 (.csv) text files line by line with a single entries on each line and then outputs NON-duplicate lines to a third (.csv) text file, the problem is the lines may be exactly the same, but in different order in the 2 text files, so sourcefile1... (11 Replies)
Discussion started by: unclecameron
11 Replies

8. Shell Programming and Scripting

Compare two files and output diff to third file

I have made several attempts to read two files of ip addresses and eliminate records from file1 that are in file2. My latest attempt follows. Everything works except my file3 is exactly the same as file1 and it should not be. # !/usr/bin/bash # # NoInterfaces # Utility will create a file... (8 Replies)
Discussion started by: altamaha
8 Replies

9. Shell Programming and Scripting

diff 2 files; output diff's to 3rd file

Hello, I want to compare two files. All records in file 2 that are not in file 1 should be output to file 3. For example: file 1 123 1234 123456 file 2 123 2345 23456 file 3 should have 2345 23456 I have looked at diff, bdiff, cmp, comm, diff3 without any luck! (2 Replies)
Discussion started by: blt123
2 Replies
Login or Register to Ask a Question