Re: [問題] 請問怎麼將一個檔案切割成許多檔案?
※ 引述《akirati (瘋了)》之銘言:
: 檔案像是這樣子
: total energy
: ---------------
: 1123 12 3 212 313
: 12 23 123 13 313
: total energy
: ---------------
: 123 12 13 215 411
: 1 212 212112 2222
: ...
: 有一行total energy會一直重複
: 想說要從total energy這行將檔案切割成數個檔案
: 不知道要怎麼去下手@@
===== /tmp/aaa.txt ====
total energy
---------------
1123 12 3 212 313
12 23 123 13 313
total energy
---------------
123 12 13 215 411
1 212 212112 2222
...
=======================
===== /tmp/test.pl ====
#!/usr/bin/perl
$File = '/tmp/aaa.txt';
$Mark = 'total energy';
sub toFile {
if(@_) {
$Part = "$File." . ++ $No;
open(OUT, "> $Part") or die "$Part: $!\n";
print OUT @_;
close(OUT);
}
}
open(IN, $File) or die "$File: $!\n";
while(<IN>) {
if(/$Mark/) { toFile @Temp; undef @Temp; }
push(@Temp, $_);
}
if(@Temp) { toFile @Temp; undef @Temp; }
close(IN);
=======================
將產生 /tmp/aaa.txt.1 /tmp/aaa.txt.2 內容各為...
===== /tmp/aaa.txt.1 ==
total energy
---------------
1123 12 3 212 313
12 23 123 13 313
=======================
===== /tmp/aaa.txt.2 ==
total energy
---------------
123 12 13 215 411
1 212 212112 2222
...
=======================
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.232.236.185
推
09/04 13:22, , 1F
09/04 13:22, 1F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):