First days dealing with Perl and blocked already :)
Here's the situation: a file is updated in folder A but also exists in folders B, C & D and, to make it easier, it can be different in all of them so I can't just do a diff. New lines that are meant to be copied to the other files are identified by a flag, for instance #I, at the end of the line.
File before being updated looks like this:
First line
Second line
Fifth line
After being updated it looks like this:
First line
Second line
Third line #I
Fourth line #I
Fifth line
Sixth line #I
What I need to do is to search for the "Second line" on the other files, insert lines tagged with #I - in the order they were inserted - then search for the "Fifth line" and insert the "Sixth line #I".
In this example they are all consecutive but in the files I need to update there can be several lines between the first update block and the second (and the third and etc etc).
The files that will be updated can be sh scripts, awk scripts, plain text files, etc., the script is supposed to be generic. The script will have two entry parameters, the updated file and the file to be updated.
Any hints on how to do this are are welcome. I can provide the code I have so far - close but not working yet - if needed.
Thanks,
João
PS: Here's what I have so far
# Pass the content of the file $FileUpdate to the updateFile array
@updateFile = <UPD>;
# Pass the content of the file $FileOriginal to the originalFile array
@originalFile = <ORG>;
# Remove empty lines from the array contained on the updated file
@updateFile = grep(/\S/, @updateFile);
# Create an array that will contain the modifications and the line
# prior to the first modification.
@modifications = ();
# Counter initialization
$i = 0;
# Loop the array to find out which lines are flagged as new and
# which lines immediately precede those
foreach $linha (@updateFile) {
# Remove \n characters
chomp($linha);
# Find the new lines flagged with #I
if ($linha =~ m/#I$/) {
# Verify that the previous line is not flagged as updated.
# If it is not, it means that the update starts here.
unless ($updateFile[$i-1] =~ m/#I$/) {
print "Line where the update starts $updateFile[$i-1]\n";
# Add that line to the array modifications
push(@modifications, $updateFile[$i-1]);
} # END OF unless
print "$updateFile[$i]\n";
# Add the lines tagged for insertion into the array
push(@modifications, $updateFile[$i]);
} # END OF if ($linha =~ m/#I$/)
# Increment the counter
$i = $i + 1;
} # END OF foreach $linha (@updateFile)
foreach $modif (@modifications) {
unless ($modif =~ m/#I$/) {
foreach $original (@originalFile) {
chomp($original);
if ($original ne $modif) {
push (@newOriginal, $originalFile[$n]);
}
elsif ($original eq $modif) { #&& $modif[$n+1] =~ m/#I$/) {
push (@newOriginal, $originalFile[$n]);
last;
}
$n = $n + 1;
}
}
if ($modif =~ m/#I$/) {
push (@newOriginal, $modifications[$m]);
}
$m = $m + 1;
}
The result obtained is almost the one I want but not yet.