perl分割文件为多个文件的脚本

发布时间:2021-01-06编辑:脚本学堂
perl分割文件为多个文件

perl实现:分割一个文件为多个TXT文件的问题。
文件内容:
IP,MOIndex,className,fdn,latitude,longitude,matchVersion,name,neID,neType,neVersion,realLatitude,realLongitude,vendorName,
10.133.10.21,18,BSC6000NE,.3221229568.3221233664.3221291023,281.0,524.0,iManagerM2000_BSC6000_MATCH_CHS_V200R008C12SPC025,BJ-HW-BSC03,18,BSC6000,BSC6000V900R008C12,0DN,0DE,Huawei,


BSCIndex,GrpIndex,MOIndex,className,fdn,name,neID,
0,0,18_0_0,BSC6000BCGrp,.3221229568.3221233664.3221291023.3221581834,BC集合,18,


MOIndex,className,fdn,name,neID,
18,BSC6000Equipment,.3221229568.3221233664.3221291023.3221393419,Bsc6000Equipment,18,


MOIndex,className,fdn,name,neID,
18,BSC6000Function,.3221229568.3221233664.3221291023.3221397515,Bsc6000Function,18,


BSCIndex,GrpIndex,MOIndex,className,fdn,name,neID,
0,0,18_0_0,BSC6000HDLCLinkGrp,.3221229568.3221233664.3221291023.3221721098,HDLC链路集合,18,

说明: txt文件中有很多类似的行列,规律是每隔两行分别有两行数据,第一行是字段名称,第二行是数据。

要求:  
1、读入文件。然后搜索每一个字段行,在行中找到className的位置,然后按照这个位置,记录每个className字段对应的数据行中的名称,一般是叫BSC6000.....的这个。
2、然后按照这个名称,建立文本文件,把相关的字段行和数据行写入新的文件。
3、写入格式仍然保持原来这样,只是不需要中间的那两行空行拉。

网上找到的解决方法,供大家参考。
#语言:perl
#程序test.pl
#用法:test.pl  参数
#其中,参数为你要处理的文件路径
#输出文件都是className.txt,你也可以改后缀名,这个在程序中很容易看见
 

复制代码 代码如下:

#!/usr/bin/perl
use strict;

if (!defined $ARGV[0]) {
        print "用法提示:test.pl [处理的文件路径]ntteg.test.pl c:a.txtn";
        exit(-1);
}
if (!-e "$ARGV[0]") {
        print "文件($ARGV[0])不存在!n";
        exit(-1);
}

my $path = $ARGV[0];
my %Content;
if (!open FILE,"<$path") {
        print "无法打开权限!n";
        exit(-1);
}
else{
        my $flag = 0;
        my $i;
        my ($temp,@line,$line);
        foreach (<FILE>) {
                chomp;
                $line = $_;
#                $_ = s/,$//;
                next if ($_ =~ /^s+$/);
                @line = split /,/;

                if ($flag == 0) {
                        $temp = $line."n";
                        for ($i = 0;$i <= $#line;$i++){
                                if ($line[$i] =~ /className/i){
                                        $flag = 1;
                                        last;
                                }
                        }
                }
                else{
                        $Content{$line[$i]} .= $temp.$line."n";
                        $flag = 0;
                }
        }
        close FILE;
}

foreach my $name (keys %Content) {
        if (!open TXT,">$name.txt") {
                print "无法写:$name.txt";
                next;
        }
        else{
                print TXT $Content{$name};
                close TXT;
        }
}

print "succeed!n";
#!/usr/bin/perl
use strict;

if (!defined $ARGV[0]) {
        print "用法提示:test.pl [处理的文件路径]ntteg.test.pl c:a.txtn";
        exit(-1);
}
if (!-e "$ARGV[0]") {
        print "文件($ARGV[0])不存在!n";
        exit(-1);
}

my $path = $ARGV[0];
my %Content;
if (!open FILE,"<$path") {
        print "无法打开权限!n";
        exit(-1);
}
else{
        my $flag = 0;
        my $i;
        my ($temp,@line,$line);
        foreach (<FILE>) {
                chomp;
                $line = $_;
#                $_ = s/,$//;
                next if ($_ =~ /^s+$/);
                @line = split /,/;

                if ($flag == 0) {
                        $temp = $line."n";
                        for ($i = 0;$i <= $#line;$i++){
                                if ($line[$i] =~ /className/i){
                                        $flag = 1;
                                        last;
                                }
                        }
                }
                else{
                        $Content{$line[$i]} .= $temp.$line."n";
                        $flag = 0;
                }
        }
        close FILE;
}

foreach my $name (keys %Content) {
        if (!open TXT,">$name.txt") {
                print "无法写:$name.txt";
                next;
        }
        else{
                print TXT $Content{$name};
                close TXT;
        }
}

print "succeed!n";