2014-10-31 84 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 

您還沒有表示是否要在###線路輸出或沒有,所以我離開了他們。

根據您所使用的操作系統以及您的潛在文件名包含的內容,您可能需要過濾它們並用下劃線替換特殊字符(或只刪除特殊字符)。