2014-10-31 91 views
-1

我想借助正则表达式在特定行中拆分大文件以分割小文件。任何帮助? 我的代码在做这个工作,但它也创建了一个空文件。避免创建空文件

#!/usr/local/lib/perl/5.14.2 

open(INFILE, 'test.txt'); 
@lines = <INFILE>; 
$file = "outfile"; 
for ($j = 0; $j <= $#lines; $j++) { 
    open(OUTFILE, ">", $file . $j); 
    $file_name = $file . $j; 
    #print "file is $file_name\n"; 
    $i = 0; 
    while (@lines) { 
     $_ = shift @lines; 
     chomp; 
     $i++; 
     if ($_ =~ /^###\s*(.*)\s*###/ && $i > 1) { 
      unshift @lines, "$_\n"; 
      print "$filename\n"; 
      last; 
     } 
     print OUTFILE "$_\n"; 
    } 
    close(OUTFILE); 
} 
close(INFILE); 

我的输入文件包括:

------------- 
### abcd hdkjfkdj #### 
body 1 dsjklsjdfskl 
### zyz fhid ### 
abcdksdsd djnfkldsfmnsldk ;lkjfkl 
--------------------------- 

它正在创建3个outfiles称为outfile0outfile1outfile2。但outfile0是空的我想避免这种情况。

+1

那我们怎么知道你的代码出了什么问题,直到我们看到它? – 2014-10-31 08:37:26

+0

我想添加我的代码,但系统不允许我。 – 2014-10-31 08:39:18

+0

哪个系统?你的意思是StackOverflow?只需编辑您的帖子并粘贴代码即可。 – 2014-10-31 08:39:58

回答

3

解决这个问题的方法是打开文件,只是为了响应找到的行。你的程序将打开一个新文件,无论这是为什么它有一个空输出文件

这是一个重写的工作。我还删除了临时@lines阵列

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

open(my $file,"<", "test.txt") || die $!; 
my $counter=1; 
my $out; 

while(<$file>) { 
    if (/###\s*(.*)\s*###/) { 
    open($out, ">", "outfile$counter") || warn "outfile$counter $!"; 
    $counter++; 
    } 
    print $out $_ if $out; 
} 
+0

谢谢大家,我们可以重新命名oufile0 ... etc。或者在if条件中用eregex- $ 1的值命名生成的文件吗? if(/###\s*(.*)\s*###/){ in the above(。*) - > $ 1我们可以用$ 1的值来命名文件吗? – 2014-10-31 10:11:00

+0

我也有文件命名,还有一个疑问。我们可以在分割功能中使用多个条件吗? – 2014-10-31 10:38:55

+0

你可以用$ 1来命名文件吗?是“我们可以使用多种条件吗?”?不明白你的意思。做另一个SO问题 – Vorsprung 2014-10-31 13:22:13

0

如果你想使用###块作为文件标题之间的材料,你可以设置文件名时,你在做与该行的模式匹配###块。

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

open my $fh, '<', 'my_file.txt' or die "Could not open file: $!"; 

# initialise a variable that will hold the output file handle 
my $out; 
while (<$fh>) { 
    # capture the title between the # signs 
    if (/##+ (.*?) ##+/) { 
     open $out, '>', $1.".txt" or die "Could not create file $1.txt: $!"; 
    } 
    elsif ($out) { 
     print $out $_; 
    } 
    else { 
     # if $out is not set, we haven't yet encountered a title block 
     warn "Error: line found with no title block: $_"; 
    } 
} 

样品输入:

Text files containing their own name 
### questions-1 #### 
Why are a motorcycle's front brakes more effective than back? 
Is it possible to make a gradient follow a path in Illustrator? 
Text files containing their own name 
### questions-2 ### 
Why does Yoda mourn the Jedi after order 66 is executed? 
what are the standard gui elements called? 
Flybe just cancelled my return flight. Will they refund that part of the trip? 
### questions-3 ### 
Merge two arrays of ElementModels? 
Is this set open or closed? 

输出:三个文件,questions-1.txtquestions-2.txtquestions-3.txt,含有适当的行。例如问题-1.TXT:

Why are a motorcycle's front brakes more effective than back? 
Is it possible to make a gradient follow a path in Illustrator? 
Text files containing their own name 

您还没有表示是否要在###线路输出或没有,所以我离开了他们。

根据您所使用的操作系统以及您的潜在文件名包含的内容,您可能需要过滤它们并用下划线替换特殊字符(或只删除特殊字符)。