Diff of 2 files using perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Diff of 2 files using perl
# 1  
Old 04-25-2013
Code 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
Code:
Alpha    156
Beta     255
Gama    6987
segma   4578

File2 new date
Code:
Alpha    156
Beta     255
Gama    1000
segma   4578

Result
File2 has an update
Code:
Gama 1000

# 2  
Old 04-25-2013
Here is a solution using awk program:
Code:
awk 'NR==FNR{A[$1]=$2;next}$1 in A&&$2!=A[$1]' file1 file2

Disregard if you are looking for a perl solution.
# 3  
Old 04-25-2013
I have a perl script so i want to add the below step in perl .
# 4  
Old 04-25-2013
Use hashes %_1 and %_2 with their members. Fill %_3. Report from %_3.

Code:
use strict ;

my $infile1 = q[File1];
my $infile2 = q[File2];
my %_1 = () ;
my %_2 = () ;
my %_3 = () ;

#now firstfill...

open (I, '<' . $infile1) or die @! ;
open (II, '<' . $infile2) or die @! ;

while (<I>) {
 chomp ;
 m/^(.+?)\s+([0-9\+\-]+)$/ ;
 $_1{$1} = $2 ;
 m/./; #reset regex
}

while (<II>) {
 m/./ ; #reset regex
 chomp ;
 m/^(.+?)\s+([0-9\+\-]+)$/ ;
 $_2{$1} = $2 ;
}

#fill....

push %_3, map { ($_, $_2{$_}) } grep { $_1{$_} != $_2{$_} } keys %_2 ; 

#now report ...

print qq[\n], $_, qq[\t\t], $_3{$_} for sort keys %_3 ;

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Diff 3 files, but diff only their 2nd column

Guys i have 3 files, but i want to compare and diff only the 2nd column path=`/home/whois/doms` for i in `cat domain.tx` do whois $i| sed -n '/Registry Registrant ID:/,/Registrant Email:/p' > $path/$i.registrant whois $i| sed -n '/Registry Admin ID:/,/Admin Email:/p' > $path/$i.admin... (10 Replies)
Discussion started by: kenshinhimura
10 Replies

2. Shell Programming and Scripting

.procmailrc and uudeview (put attachments from diff senders to diff folders)

Moderator, please, delete this topic (1 Reply)
Discussion started by: optik77
1 Replies

3. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: script2010
4 Replies

4. Shell Programming and Scripting

diff bw two files

Hi All, I have two files which look as below File1 serial="1" name="abc" type="employee" field="IT" serial="2" name="cde" type="intern" field="Marketing" serial="3" name="pqr" type="contractor" field="IT" serial="4" name="xyz" type="employee" field="Sales" File2 serial="1"... (3 Replies)
Discussion started by: grajp002
3 Replies

5. Shell Programming and Scripting

diff of files

Hi, I have 2 files.I want to check if file1 is contained in file2. A.txt: ----- AAA BBB B.txt: ------ CCC AAA BBB DDD I want to check if A.txt is contained in B.txt. Can it be done using SED ? (12 Replies)
Discussion started by: giri_luck
12 Replies

6. Shell Programming and Scripting

Diff b/w 2 files

Hi Masters, I have two files named file1 and file2. Both the files contains the same contents with some difference in comments,space.But no content change. I tried to find the diff between the two files to make sure that contents are same. For that i tried diff -ibw file1 file2 But... (1 Reply)
Discussion started by: ecearund
1 Replies

7. Shell Programming and Scripting

compare 2 coloum of 2 diff files using perl script

Hi, i am new to perl scripting.. i am still learing it.. i am asked to write a perl script which should compare 2 coloums of 2 different files. if those 2 coloumn are same the script should store the both the lines in 2 diff files. these are files, file 1: 21767016 226112 char 19136520... (3 Replies)
Discussion started by: vasuki
3 Replies

8. Shell Programming and Scripting

Find duplicates from multuple files with 2 diff types of files

I need to compare 2 diff type of files and find out the duplicate after comparing each types of files: Type 1 file name is like: file1.abc (the extension abc could any 3 characters but I can narrow it down or hardcode for 10/15 combinations). The other file is file1.bcd01abc (the extension... (2 Replies)
Discussion started by: ricky007
2 Replies

9. Shell Programming and Scripting

perl package directories - what if the script is diff DIR to the one contain *.pm?

Hi there, say the package is in the ~/ and it's ~/packageFoo.pm I can use usePackage.pl in ~/ (~/usePackage.pl). Now, if I move it to ~/subDIR/usePackage.pl, the script won't work because it's not in the same DIR with packageFoo.pm How can i fix it? Thanks Gusla (1 Reply)
Discussion started by: gusla
1 Replies

10. 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