用于通过将字符匹配到多个文件来拆分单个文件的perl代码

时间:2014-04-17 23:00:52

标签: perl

我想将一个大型数据文件拆分成多个文件,而这些文件与“' ^”相匹配。字符。

#!/usr/bin/perl -w  
use strict;  
print "enter the data file name";  
chomp( my $a=<STDIN> );  
open (<READ>,"$a")| "error";  
while ($line=<READ>)  
 {  
  my @array=split("        ",$line) unless ^ ;  
分割数据文件后

。总共将创建23个文件

4 个答案:

答案 0 :(得分:1)

来自saiprathapreddy.obula的答案略有不同:

use warnings;
use strict;

my ($file) = @ARGV;

open(my $input, "<$file");

local $/ = "^";

my $i = 0;

while(<>){
    chomp;

    $i++;

    open(my $output, ">file$i.txt");
    print $output "$_"; 
}

答案 1 :(得分:0)

use strict;

open(FILE,'AUTOSAR.txt');
local $/;
my $var = <FILE>;
my @arr = split('\^',$var);
my $i=0;

foreach (@arr) {
    $i++;
    open(FILE1,">$i.txt");
    print FILE1 $_;
    close FILE1;
}

close FILE;

答案 2 :(得分:0)

$,="";
$"="";
my $i=1;
open OUT ,">DATA_${i}.txt";
while(<>){
    chomp;
    my @F=split(/\^/);
    if(@F==1){
            print OUT $_,"\n";
    }
    elsif(@F>1){
            $i++;
            close OUT;
            open OUT ,">DATA_${i}.txt";
            print OUT "@F[1..$#F] \n";
    }
}
close OUT;

答案 3 :(得分:0)

这是saiprathapreddy.obula程序的清理和测试版本。

use strict;
use warnings;

open(FILE,'AUTOSAR.txt');

local $/;
my $var = <FILE>;
close FILE;

my @arr = split('\^',$var);
my $i=0;

foreach (@arr) {
    $i++;
    open(FILE1,">$i.txt");
    print FILE1;
    close FILE1;
}