regex - How to merge diff result between two files into a third file? -
i trying write perl script following: compare 2 files same name different source dirs, meaning:
diff source_dir_1/file_1 source_dir_2/files_2
and each changed line in file_2 (compared file_1) find line file_1 (that has been changed in file_2) @ third file: source_dir_3/file_3, , replace line line file_2.
for example, for:
file_1:
my name shahar
hello world
nice meet
file_2
my name shahar
goodbye world
nice meet
file_3:
this line can different
hello world
nice meet
the resulting file_3 be:
file_3_after_script:
this line can different
goodbye world
nice meet
i'm having problem writing it, because sometime 1 line in file_1 replaced several lines in file_2,
do have suggestions how should approach problem?
your goal unclear. include code you've tried in question on so.
nevertheless, of course possible open 3 files simultaneously , perform logic on them.
the following shows best interpretation of goal. cycles through lines of each file, , prints line file2 if file1 , 2 different, otherwise prints file3.
use strict; use warnings; use autodie; $file1 = '...'; $file2 = '...'; $file3 = '...'; #open $fh1, '<', $file1; #open $fh2, '<', $file2; #open $fh3, '<', $file3; # open strings temporarily make script self-contained: open $fh1, '<', \ "my name shahar\nhello world\nnice meet\n"; open $fh2, '<', \ "my name shahar\ngoodbye world\nnice meet\n"; open $fh3, '<', \ "this line can different\nhello world\nnice meet\n"; while (! grep eof $_, $fh1, $fh2, $fh3) { $line1 = <$fh1>; $line2 = <$fh2>; $line3 = <$fh3>; print $line1 ne $line2 ? "file2: $line2" : "file3: $line3"; } print "file3: $_" while (<$fh3>); # print rest of file (if any)
outputs:
file3: line can different file2: goodbye world file3: nice meet
Comments
Post a Comment