Perlを扱い、すでにブロックされている最初の日:)
状況は次のとおりです。ファイルはフォルダーAで更新されますが、フォルダーB、C、Dにも存在します。簡単にするために、ファイルはすべてのフォルダーで異なる可能性があるため、差分を実行することはできません。他のファイルにコピーされることを意図した新しい行は、行の終わりにあるフラグ(たとえば、 #I )によって識別されます。
更新前のファイルは次のようになります。
First line
Second line
Fifth line
更新後は次のようになります。
First line
Second line
Third line #I
Fourth line #I
Fifth line
Sixth line #I
私がする必要があるのは、他のファイルで「2行目」を検索し、#Iでタグ付けされた行を挿入された順序で挿入してから、「5行目」を検索して「6行目#I」を挿入することです。 。
この例では、それらはすべて連続していますが、更新する必要のあるファイルでは、最初の更新ブロックと2番目(および3番目など)の間に複数の行が存在する可能性があります。
更新されるファイルは、shスクリプト、awkスクリプト、プレーンテキストファイルなどです。スクリプトは汎用であると想定されています。スクリプトには、更新されたファイルと更新されるファイルの2つのエントリパラメータがあります。
これを行う方法についてのヒントは大歓迎です。必要に応じて、これまでに使用したコードを提供できます。近いですが、まだ機能していません。
ありがとう、
João
PS:これが私がこれまでに持っているものです
# 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;
}
得られた結果は、私が望むものとほぼ同じですが、まだです。