0
我一直在用perl語法掙扎。我試圖學習足夠的條件替換另一個項目的源代碼,但仍然遇到麻煩。我寫了一個簡單的腳本來測試我的問題,並且它運行良好。我在一個文件上試過了,它不再有效。該文件包含與我的測試字符串相同的文本。perl語法用於匹配文件中的字符串
我想我必須在文件中做一些特殊的閱讀,但我不知道是什麼。
test.sh:
#!/usr/bin/perl
use strict;
use warnings;
my $matching = 'Logger\.getInstance\(\)\.addEntry\(((.|\n)*),((.|\n)*),((.|\n)*),((.|\n)*)\)';
my $replacement = '"Logger\.getInstance\(\).addEntry\($1,$7\)"';
my $test_str = "Logger\.getInstance\(\)\.addEntry\(\"error\", \"Thing2D\.PATH_TYPE\", new Object\(\) \{\}\.getClass\(\)\.getEnclosingMethod\(\)\.getName\(\), \"New PATH_TYPE was added and not filled in\.\");";
print "----\nstring:\n" . $test_str . "\n\nreplaced:\n\n";
$test_str =~ s/$matching/$replacement/ee;
print "\n\n" . $test_str . "\n\n\n";
die qq[Usage: perl $0 <subject_file>\n] unless @ARGV == 1;
open my $fh_subject, qq[<], shift @ARGV or die qq[Cannot open input file: $!\n];
my $subject_content;
{
local $/ = undef;
$subject_content = <$fh_subject>;
}
print "----\nString:\n" . $subject_content . "\n\nreplaced:\n\n";
$subject_content =~ "s/$matching/$replacement/ee";
print $subject_content;
的test.txt:
Logger.getInstance().addEntry("error", "Thing2D.PATH_TYPE", new Object() {}.getClass().getEnclosingMethod().getName(), "New PATH_TYPE was added and not filled in.");
和命令行:
./test.sh test.txt
謝謝大家的幫忙!
我知道這是簡單的東西!非常感謝你。我正在努力爲那些重複性的工作學習一點珍珠。這有助於很多! – vextorspace