使用换行做为分界
忽略注释行#
分割存入新指定的文件中code
test.lst
wwdg/prescaler syscfg/test1 syscfg/test2 uart/uart7/uart7_dma1 uart/uart7/uart7_dma2
genlst.pl
#!/usr/bin/perl use warnings; ########################################### #cut up file # #cut_flie.pl test.lst ########################################### my $find_ip =0; my $find_begin = 0; my $line_index = 0; my $lstname = "error"; my $past_line = "error"; my $infile= $ARGV[0]; open(INFILE, " $infile") || die ("Could not open file $infile ! \n"); if(-z $infile){ print "\nFile is blank!\n"; exit; } #覆盖lst中的文件,需手动删除已经存在的文件 my $dir = "lst"; #建立存放目录 if(-e $dir){ } else{ mkdir $dir; } #删除lst中的文件 #my $dir = "lst"; #建立存放目录 #if(-e $dir){ # unlink glob "$dir/* $dir/.*" #} #else{ # mkdir $dir; #} # get file lines amount my @fileline = <INFILE>; #获得文件的行数 my $line_num = @fileline; close INFILE; unlink "temp.lst"; open(TEMPA, ">> temp.lst") || die ("Could not open file temp.lst! \n"); open(INFILE, " $infile") || die ("Could not open file $infile ! \n"); while ($line = <INFILE>){ $line_index ++; chomp($line); if($line =~ /^#/) { #find "#" at the beginning, don't care next; } if($line =~ /^(\w+)[\s\$\W]*/){ #匹配捕获字符串,非空行 if($past_line =~ /^$/){ #上一行为空最为分界 print TEMPA "\n\n"; my $full_lstname = $lstname."_lst"; print "$dir\/$full_lstname\n"; rename "temp.lst", "$full_lstname"; close TEMPA; #完成一个文件的分割 system"mv $full_lstname lst"; print "\n\n"; unlink "temp.lst"; open(TEMPA, ">> temp.lst") || die ("Could not open file temp.lst! \n"); #继续进行下一个文件处理 } $line =~ /^(\w+)[\s\$\W]*/ ; #正则处理,获得文件名 print TEMPA "$line \n"; print "$line \n"; $lstname = $1; } if($line_index == $line_num){ #单独处理最后行状况 print TEMPA "\n\n"; #print "\n\n"; my $full_lstname = $lstname."_lst"; print "$dir\/$full_lstname\n"; rename "temp.lst", "$full_lstname"; close TEMPA; system"cp $full_lstname lst"; unlink "$full_lstname"; } if($line =~ /^\s*$/){ if($line_index == 1){ next; } if($past_line =~ /^$/){ next; } } $past_line = $line; #保存上一行状况 } close INFILE; close TEMPA; unlink "temp.lst"; unlink "lst/error_lst";