每个 div 应作为单独的文件分开。
输入.txt
[[div]]
line 1
line 2
...
[[/div]]
[[div]]
line 3
line 4
line 5
...
[[/div]]
[[div]]
line 6
line 7
...
[[/div]]
文件名.txt
fm.html
chap01.html
bm.html
需要输出
fm.html
<html>
<body>
line 1
line 2
...
</body>
</html>
chap01.html
<html>
<body>
line 3
line 4
line 5
...
</body>
</html>
bm.html
<html>
<body>
line 6
line 7
...
</body>
</html>
我现在尝试过的编码..但它返回所有文件中的最后一个 div ......并且需要添加元......请需要解决方案
#!/usr/bin/perl
open(REDA,"filename.txt");
@namef=<REDA>;
open(RED,"input.txt");
open(WRITX,">input1.txt");
while(<RED>)
{
chomp($_);
$_="$_"."<cr>";
print WRITX $_;
}
close(RED);
close(WRITX);
open(REDQ,"input1.txt");
open(WRITQ,">input2.txt");
while(<REDQ>)
{
$_=~s/\[\[div\]\]<cr>/\n\[\[div\]\]/gi;
print WRITQ $_;
}
close(REDQ);
close(WRITQ);
open(REDE,"input2.txt");
while(<REDE>)
{
foreach $namef (@namef)
{
chomp($namef);
$namef=~s/\.[a-z]+//gi;
open(WRIT1,">$namef.html");
if(/\[\[div\]\]/i)
{
chomp($_);
$_=~s/<cr>/\n/gi;
print WRIT1 $_;
}
}
}
close(REDA);
close(REDE);
close(REDX);
close(WRIT1);
system ("del input1.txt");
system ("del input2.txt");