文件里是多个连贯的脚本,需要将它们拆开
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 "$_";
}
}