CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
comparing the difference in one text file

 



kenshin
Novice

May 31, 2009, 2:48 AM

Post #1 of 13 (3358 views)
comparing the difference in one text file Can't Post

OK I was searching for an example but couldnt find one. Well I have this program:


Code
#!/usr/bin/perl -w 

use strict;
use warnings;

my @filelist = </home/n/*>;

my $rec = "/home/record.txt";
open(FILE, "+>$rec") or die "Can't open $rec: $!";
foreach(@filelist){
if($_ =~ m/(~|\.)/){
}else{
my $data =`egrep "A|MX|PTR|CNAME" $_`;
print FILE "$_\n\n$data----------------------------------------------\n\n";
}
}


What it does it will print records in "records.txt" I the program will run every 5 min using a cronjob, so if a file Added,Deleted, or modified a file in /home/n/* it will show it in the records.txt.


Thanks


FishMonger
Veteran / Moderator

May 31, 2009, 5:52 AM

Post #2 of 13 (3355 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

And your question is?


kenshin
Novice

May 31, 2009, 6:01 AM

Post #3 of 13 (3352 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

My question, is there a way to compare changes in one file and add whats changed in the same file?


FishMonger
Veteran / Moderator

May 31, 2009, 6:55 AM

Post #4 of 13 (3350 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

Your question isn't very clear.

Are you saying that you want to scan/monitor the files in a directory and any changes made to those files get duplicated in the record.txt file?

If so, then you'll need an additional file that stores the byte offset each time you read the file and then seek to that point on the next time it's read. Then from that point to eof, you append that data to the record.txt file.

See:
perldoc -f sysopen
perldoc -f sysread
perldoc -f syswrite


If that's not what you're asking, then please rephrase your question.


(This post was edited by FishMonger on May 31, 2009, 6:56 AM)


kenshin
Novice

May 31, 2009, 10:50 PM

Post #5 of 13 (3334 views)
Re: [FishMonger] comparing the difference in one text file [In reply to] Can't Post

Sorry is if am not clear, Ok in this file it will print in records.txt some line's.

What I want is that in the Second run is, to create a new file then compare the new file with records.txt. this is an "If statement"

after comparing it will over write records.txt, with a comment on each line if there is something deleted, modified, or added.

and only for modified it shows what was the old record and what is the new.

Thanks a lot


FishMonger
Veteran / Moderator

Jun 1, 2009, 7:34 AM

Post #6 of 13 (3327 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

Please post some example lines from records.txt and the corresponding lines from the new file that needs to be compared and resulting lines with comments after the comparison.


kenshin
Novice

Jun 1, 2009, 11:15 PM

Post #7 of 13 (3318 views)
Re: [FishMonger] comparing the difference in one text file [In reply to] Can't Post


Code
"records.txt" 

/home/H

asdfasdfasdfasdf.com A 0123984304918
alsdjflkasjdf CNAME 10294809238

----------------------------------------------



Code
 
"newrecord.txt"

/home/H

asdfasdfasdfasdf.com A 01239843
asdfasdfasdf A 01239480123
---------------------------------------------



Code
"Result.txt" 
/home/H

"record" [asdfasdfasdfasdf.com A 0123984304918] "changed to" [asdfasdfasdfasdf.com A 01239843]
"record" [alsdjflk CNAME 10294809238] "Deleted"
"record" [asdfasdfasdf A 01239480123] Added

---------------------------------------------


(This post was edited by kenshin on Jun 1, 2009, 11:19 PM)


KevinR
Veteran


Jun 7, 2009, 10:43 PM

Post #8 of 13 (3295 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

Very difficult task to compare files like you are wanting to do. If you are comparing the same line numbers in one file against the other you can tell if the line has been changed, but if a line is deleted all the lines in one file will appear to be altered after that.
-------------------------------------------------


kenshin
Novice

Jun 7, 2009, 11:35 PM

Post #9 of 13 (3291 views)
Re: [KevinR] comparing the difference in one text file [In reply to] Can't Post

I actually did it using the

Code
`diff --unified=1000 $rec $rec2 | egrep -v "^---" | grep -v "^+++" | grep -v "^\@\@"`


Worked Fine :)

now i am trying to send the results using Net::Syslog and i get this weird error though i know I installed the Perl Autoloader

Code
Can't locate package AutoLoader for @Net::Syslog::ISA at /usr/local/share/perl/5.10.0/Net/Syslog.pm line 65.



KevinR
Veteran


Jun 7, 2009, 11:47 PM

Post #10 of 13 (3288 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

You shouldn't need to install Autoloader, it comes with perl, but you do have to install the module.
-------------------------------------------------


kenshin
Novice

Jun 8, 2009, 12:03 AM

Post #11 of 13 (3287 views)
Re: [KevinR] comparing the difference in one text file [In reply to] Can't Post

Well I installed the Net::Syslog and the Sys::Syslog and still i get the same error above.


KevinR
Veteran


Jun 8, 2009, 8:24 AM

Post #12 of 13 (3275 views)
Re: [kenshin] comparing the difference in one text file [In reply to] Can't Post

Is Net::Syslog::ISA installed?
-------------------------------------------------


kenshin
Novice

Jun 9, 2009, 9:38 PM

Post #13 of 13 (3267 views)
Re: [KevinR] comparing the difference in one text file [In reply to] Can't Post

I tried that and i got this,


Quote
Warning: Cannot install Net::Syslog::ISA, don't know what it is.


well now i am using Net::Dev::Tools::Syslog because Net::Syslog seems out of date.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives