0

每个 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");
4

4 回答 4

1

如果您确定这些[[div]]部分由空行分隔,您可以使用 Perl 的段落模式 slurp,它将文件分成由一个或多个空行分隔的块。以下代码(经过测试)可以满足您的需求。在当前目录包含相关文件的终端中执行以下命令:

perl -n00 -e '
    BEGIN{ #Executed before input.txt is read
        open $f,"<","filename.txt";
        @names = split /\n+/,<$f> #Split is needed because we changed the input record separator
    }

    # The following is executed for each "paragraph" (div section)
    s!\[\[div\]\]\n!<html>\n<body>\n!; # substitute <html>\n<body\n instead of [[div]]
    s!\[\[/div\]\]\n!</body>\n</html>!; # substitute </body>\n</html> instead of [[/div]]
    $content{shift @names}=$_; #Add the modified content to hash keyed by file name

    END{ #This is executed after the whole of input.txt has been read
        for(keys %content){ #For each file we want to create
            open $of,">",$_;
            print $of $content{$_}
        }
    }
' input.txt

更新

如果要将上述代码用作 Perl 脚本,可以执行以下操作:

#!/usr/bin/env perl

use strict;
use warnings;

open my $f,'<','filename.txt' or die "Failed to open filename.txt: $!\n";
my @names;
chomp(@names=<$f>);

open my $if,'<','input.txt' or die "Failed to open input.txt: $!\n";
my %content;
while(my $paragraph=do{local $/="";<$if>}){
    $paragraph=~ s!\[\[div\]\]\n!<html>\n<body>\n!;
    $paragraph=~ s!\[\[/div\]\]\n!</body>\n</html>!;
    $content{shift @names}=$paragraph;
}

for(keys %content){
    open my $of,'>',$_ or die "Failed to open $_ : $!\n";
    print $of $content{$_}
}

将上述内容另存为 (say) split_file.pl,使其可执行,chmod +x split_file.pl然后将其运行为./split_file.pl.

于 2013-08-27T11:19:55.687 回答
1

你可以这样做:

#!/usr/bin/env perl
use strict;
use warnings;

my @file_names;
## Read the list of file names
open(my $fh,"$ARGV[0]");
while (<$fh>) {
    chomp; #remove new line character from the end of the line
    push @file_names,$_;
}

my $counter=0;
my ($file_name,$fn);
## Read the input file
open($fh,"$ARGV[1]");
while (<$fh>) {
    ## If this is an opening DIV, open the next output file,
    ## and set $counter to 1.
    if (/\[\[div\]\]/) {
    $counter=1;
    $file_name=shift(@file_names);
    open($fn, '>',"$file_name");
    }
    ## If this is a closing DIV, print the line and set $counter back to 0
    if (/\[\[\/div\]\]/) {
    $counter=0;
    print $fn $_;
    close($fn);
    }
    ## Print into the corresponding file handle if $counter is 1
    print $fn $_ if $counter==1
}

将脚本另存为foo.pl并像这样运行它:

perl foo.pl filename.txt Input.txt 
于 2013-08-27T13:04:30.543 回答
0

filename.txt在 Perl 中,您可以像这样遍历文件的内容:

#!/usr/bin/perl

# somescript.pl

open (my $fh, "<", "filename.txt");
my @files = <$fh>;
close ($fh);

foreach my $file (@files) {
    print "$file";
}

将上述内容放在一个名为 的文件中somescript.pl,使其可执行,chmod +x somescript.pl然后运行它:

$ ./somescript.pl 
fm.html
chap01.html
bm.html

您可以看到它现在正在读取文件filename.txt并将每一行打印到屏幕上。我把剩下的留给你试试。如果您遇到困难,请寻求帮助。

我将使用与读取文件相同的方法来读取filename.txt文件input.txt

于 2013-08-27T08:55:36.590 回答
0

用更惯用的 Perl 编写它,你可能会得到这样的结果:

#!/usr/bin/perl

use strict;
use warnings;

# First argument is the name of the file that contains
# the filenames.
open my $fn, shift or die $!;
chomp(my @files = <$fn>);

# Variable to contain the current open filehandle
my $curr_fh;
while (<>) {
  # Skip blank lines
  next unless /\S/;

  # If it's the opening of a div...
  if (/\[\[div]]/) {
    # Open the next file...
    open $curr_fh, '>', shift @files or die $!;
    # Print the opening html...
    print $curr_file "<html>\n<body>\n";
    # ... and skip the rest of the loop
    next;
  }

  # If it's the end of a div
  if (/\[\[\/div]]/) {
    # Print the closing html...
    print $curr_fh "</body>\n</html>\n";
    # Close the current file...
    close $curr_fh;
    # Unset the variable so we can reuse it...
    undef $curr_fh;
    # and skip the rest of the loop
    next;
  }

  # Otherwise, just print the record to the currently open file
  print $curr_fh $_;
}

使用两个参数调用它,包含文件名的文件名 (filename.txt) 后跟包含数据的文件名 (input.txt)。

于 2013-08-27T16:46:22.527 回答