以前はデータベース内のテーブルでしたが、現在は逆アセンブルされているいくつかのテキスト ファイルがあります。再組み立てを試みていますが、使用可能な形になったら簡単です。最初のファイル「keys.text」はラベルのリストであり、形式が一貫していません。お気に入り:
Sa 1 #
Sa 2
U 328 #*
常に文字、[スペース]、数字、[スペース]、および場合によっては記号です。これらのキーに一致するテキスト ファイルは同じで、その後にスペースで区切られた、または区切られたテキスト行が続きます。
Sa 1 # Random line of text follows.
Sa 2 This text is just as random.
U 328 #* Continuing text...
以下のコードで私がやろうとしているのは、「keys.text」のキーを .txt ファイルの同じキーと一致させ、キーとテキストの間にタブを入れることです。私は非常に基本的なものを見落としていると確信していますが、得られた結果はソースの .txt ファイルと同じように見えます。
リードや支援をよろしくお願いします!
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
open(IN1, "keys.text");
my $key;
# Read each line one at a time
while ($key = <IN1>) {
# For each txt file in the current directory
foreach my $file (<*.txt>) {
open(IN, $file) or die("Cannot open TXT file for reading: $!");
open(OUT, ">temp.txt") or die("Cannot open output file: $!");
# Add temp modified file into directory
my $newFilename = "modified\/keyed_" . $file;
my $line;
# Read each line one at a time
while ($line = <IN>) {
$line =~ s/"\$key"/"\$key" . "\/t"/;
print(OUT "$line");
}
rename("temp.txt", "$newFilename");
}
}
編集:明確にするために、結果はキーからのシンボルも保持する必要があります。したがって、それらは次のようになります。
Sa 1 # Random line of text follows.
Sa 2 This text is just as random.
U 328 #* Continuing text...