我試圖洗掉a.txt檔案的一部分,并用b.txt檔案的內容代替,同時用Perl程式對a.txt的其他行進行修改。
檔案a.txt
line1
第2行
從下面一行開始替換
從這一行開始替換
bla bla...
bla bla...
到這一行
第三行
第4行
file b.txt
replacement1
替換2
replacementn
下面是我的代碼,它沒有作業。
#!/apps/perl/5.8.3/bin/perl -w
open (INPUT, "a.txt") or die $!;
open (REPLACE, "b.txt") or die $!
open (OUTPUT, ">c.txt") or die $!
my $replace_text;
{
local $/;
$replace_text = <REPLACE>。
}
close(REPLACE)。
while (<INPUT>) {
s/line1/modified_line1/;
s/line2/modified_line2/;
if($_ =~ /replace from below line/){
while(<INPUT>){
{
local undef $/;
s/replace from this line.*to this line/$replace_text/smg;
}
s/line3/modified_line3/;
s/line4/modified_line4/。
print OUTPUT。
}
}
}
close(INPUT)。
close(OUTPUT)。
預期輸出檔案c.txt
modified_line1
修改后的第2行
替換1
替換2
replacementn
修改后的第3行
moded_line4
誰能幫助我了解我在哪里出了問題?
uj5u.com熱心網友回復:
我認為你不需要嵌套的while回圈來讀取你的輸入檔案。
一種方法是使用一個變數來控制你何時列印到輸出檔案:
use warnings;
use strict;
open (INPUT, "a.txt") or die $!;
open (REPLACE, "b.txt") or die $!
open (OUTPUT, ">c.txt") or die $!
my $replace_text;
{
local $/;
$replace_text = <REPLACE>。
}
close(REPLACE)。
my $print = 1;
while (<INPUT>) {
s/line(d)/modified_line$1/;
$print = 0 if /replace from below line/;
if (/to this line/) {
$print = 1;
$_ = $replace_text;
}
print OUTPUT if $print;
}
close(INPUT)。
close(OUTPUT)。
輸出:
modified_line1
modified_line2
替換1
替換2
replacementn
修改后的第3行
moded_line4
我還用d將你的4個行替換合并為1個
uj5u.com熱心網友回復:
盡管我很喜歡perl,但它在這里真的沒有必要:
sed -e 's/line1/modified_line1/'
-e 's/line2/modified_line2/'/span>
-e's/line3/modified_line3/'
-e 's/line4/modified_line4/'/span>
-e '/replace from below/rb.txt'
-e '/replace from below/,/to this line/d' a.txt
modified_line1
修改后的第2行
替換1
替換2
替換tn
修改后的第三條線
moded_line4
如果你確實想使用perl,我就會這樣做:
如果你想使用perl,我就會這樣做。
#!/usr/bin/env perl
use strict;
使用警告。
open my $ah, '< ', "a. txt" or die "a.txt: $!
"。
while(<$ah>) {
s/line1/modified_line1/;
s/line2/modified_line2/;
s/line3/modified_line3/;
s/line4/modified_line4/;
if( /replace from below/ ) {
系統 "cat b.txt" and exit 1;
}
next if( /replace from below/ ... /to this line/) 。
print。
}
uj5u.com熱心網友回復:
問題描述中沒有說明a.txt檔案可以有多大。發布的代碼使用了帶有修飾符/smg的正則運算式,表明OP試圖在多行文本上作業。
讓我們假設輸入檔案足夠小,可以在記憶體中讀取和處理。
為了代碼的可管理性,將替代品放入__DATA__塊中,在%substitute哈希中讀取。
根據keys %substitute建立正則運算式$re,以便在替換模式中使用。
多行替換是基于原始OP的代碼(不適用于對輸入資料的逐行讀取)。
定義了兩個子程式,將檔案內容讀入變數,并將變數資料存盤到檔案中 -- 只是為了使代碼更容易閱讀和理解。
use strict;
use warnings;
use feature 'say';
my($fname_in,$fname_repl,$fname_out) = qwa.txt b.txt c.txt/;
my %substitute = split(/[,s]/, do{ local$/; <DATA>})。)
my $re = ' (' . join('|',keys %substitute) . ')'。
my $data = read_file($fname_in);
my $replace_with = read_file($fname_repl);
$data =~ s/$re/$substitute{$1}/g;
$data =~ s/replace from below line.*? to this line/$replace_with/gsm;
save_file($fname_out,$data)。
say $data;
exit 0;
sub read_file {
my $fname = shift;
my $data;
open my $fh, '< ', $fname
or die "Couldn't open $fame";
$data = do { local $/; <$fh> };
close $fh;
return $data;
}
sub save_file {
my $fname = shift;
my $data = shift;
open my $fh, '> ', $fname
or die "Couldn't open $fame";
say $fh $data;
close $fh;
}
解釋
line1,modified_line1
line2,modified_line2
line3,modified_line3
line4,modified_line4
輸出
modified_line1
修改后的第2行
替換1
替換2
replacementn
修改后的第3行
moded_line4
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311381.html
標籤:
