文件裏是多個連貫的腳本,需要將它們拆開
1.將文件內容存入一個變量不是好辦法,有待改進
#!/usr/bin/perl -w
$tmp1 = `cat 111`;
$count = 1;
while () {
if ( $tmp1 =~ /(#!.*?)(#!.*)/s) {
$tmp2 = $1;
$tmp1 = $2;
open RESULT, "> result$count";
print RESULT "$tmp2";
close RESULT;
}
else {
open RESULT,"> result$count";
print RESULT "$tmp1";
close RESULT;
exit 0;
}
$count += 1;
}
$tmp1 = `cat 111`;
$count = 1;
while () {
if ( $tmp1 =~ /(#!.*?)(#!.*)/s) {
$tmp2 = $1;
$tmp1 = $2;
open RESULT, "> result$count";
print RESULT "$tmp2";
close RESULT;
}
else {
open RESULT,"> result$count";
print RESULT "$tmp1";
close RESULT;
exit 0;
}
$count += 1;
}
2.逐行匹配
#!/usr/bin/perl -w
while (<>) {
if (/^#!.*/) {
$count += 1;
open RESULT, ">> result$count";
print RESULT "$_";
}
else {
print RESULT "$_";
}
}
while (<>) {
if (/^#!.*/) {
$count += 1;
open RESULT, ">> result$count";
print RESULT "$_";
}
else {
print RESULT "$_";
}
}