我想通过添加某行并替换其他行来编辑文件。 我试图与包含线我的文件行的数组工作,即如何使用perl添加和替换行数组中的行
my $output_file_string = `cat $result_dir/$file`;
my @LINES = split(/\n/, $output_file_string);
我有我想要的文件中找到行的哈希表,要么更换或添加额外的行在他们之后。 我写了下面的代码来识别茨艾伦线:
foreach my $myline (keys %{ $hFiles_added{$file} }) {
foreach my $line (@LINES) {
if ($line =~ /\Q$myline\E/) {
### here should be a code for adding a new line after the current line ###
}
}
}
#### here should be a code to return the array to the output file ####
我想不出如何做加法\更换部分,以及如何保存我的编辑过的文件早在一个文件中(而不是阵列
谢谢 沙哈尔
不知道你的意思....我需要在最后用一些新的线路和一些raplaced线原始文件 – user3350919 2014-09-02 10:21:28
参见[perlfaq5(HTTP://的perldoc。 perl.org/perlfaq5.html#How-do-I-change,-delete,-or-insert-a-line-in-a-file,-or-append-to-the-beginning-of-a-file ?) – RobEarl 2014-09-02 10:33:27