根据关键字将文件内容拆分为2个数组

时间:2013-09-20 18:19:31

标签: arrays perl split

我有一个类似

的文件
line1
line2
line3
line4
.endm
line5
line6
line7
line8
.endm

我想将此文件拆分为.endm并将内容放在2个不同的数组中。输出将是

@array1=(line1,line2,line3,line4);
@array2=(line4,line6,line7,line8);

我尝试使用

@array = split(".endm",my_file.txt);
print("Array1 = $array[1]\nArray2 = $array[2]\n);

但这只是打印出最后一个.endm的行号

我也试过像

这样的东西
#! /usr/intel/bin/perl -w
use strict;

my $count=0;
open(my $fh, "<", "a.txt")|| die "can't open UTF-8 encoded filename: $!";
my @lines = <$fh>;
my @array;

LOOP: foreach my $line (@lines) {
    chomp ($line);
    $count = $count+1;
    push (@array,$line);  
    if ($line =~ m/.endm/) {
        pop (@array);
        last LOOP;
    }
}
print("Array : @array\n");
close $fh;

这个打印@array=(line1,line2,line3,line4);但是我仍然需要其他4行,这样我就可以将它放在不同的数组中

那么如何根据某个关键字分割文件?

6 个答案:

答案 0 :(得分:5)

如果您将固定字符串作为字段分隔符,则可以设置内置变量$/ - 输入记录分隔符 - 以更改Perl的readline()函数读取行的方式。

perl -MData::Dumper -we '$/=".endm\n"; chomp(@a = <>); 
      @a = map [ split /\n/ ], @a; print Dumper \@a;' file.txt

这将以两个块的形式读取文件,有效地拆分.endm\n。请注意,chomp会从字符串末尾删除$/的值。这里的map语句只是将输入拆分为换行符并将其放入数组ref中。结果数组是二维的,如Data::Dumper输出中所示:

<强>输出:

$VAR1 = [
          [
            'line1',
            'line2',
            'line3',
            'line4'
          ],
          [
            'line5',
            'line6',
            'line7',
            'line8'
          ]
        ];

完整版看起来像这样:

use strict;
use warnings;
use Data::Dumper;

$/ = ".endm\n";
chomp(my @a = <>);
@a = map [split /\n/], @a;
print Dumper \@a;

答案 1 :(得分:2)

我会保持简单,这样做:

my (@array1,@array2);
while ($_ = <>) { chomp; last if $_ eq '.endm'; push @array1, $_ }
while ($_ = <>) { chomp; last if $_ eq '.endm'; push @array2, $_ }

答案 2 :(得分:2)

#!/usr/bin/perl 
use strict;
use warnings;
use Data::Dumper;

open my $fh, '<', 'data.txt' or die "can't open data.txt: $!";

my @array;

$/ = ".endm";

while (<$fh>){
    s/\.endm$//;
    push @array, [split];
}

print Dumper(@array);

#output
$VAR1 = [
         'line1',
         'line2',
         'line3',
         'line4'
        ];
$VAR2 = [
         'line5',
         'line6',
         'line7',
         'line8'
        ];

答案 3 :(得分:1)

如果文件很小,则可以使用。

#!/usr/bin/perl 

use strict;
use warnings;

open(FILE,"input.txt");
my @lines =<FILE>;                 # Reads all lines of the file into an array
my $line  = join('',@lines);       # Join all lines together
my @temp = split(/.endm/,$line);   # Split the line using the keyword
my @array1=split(/\n/,$temp[0]);   # Splits line back 
my @array2=split(/\n/,$temp[1]);   # Splits line back

print @array1;
print "\n";
print @array2;

close(FILE);

答案 4 :(得分:1)

use 5.10.0;
use strict;
use warnings;

open( my $fh, '<', 'a.txt' ) or die "can't open filename: $!";

my @arrays;
my $array = [];

while (<$fh>) {
    chomp;
    if (/^\.endm$/) {
        push @arrays, $array;
        $array = [];
        next;
    }
    push @$array, $_;
}

local $, = ",";
say @$_ for @arrays;

$array = []是不必要的,但提高了可读性。并输出:

line1,line2,line3,line4
line5,line6,line7,line8

答案 5 :(得分:1)

在编写代码之前,您不必费心阅读文档或教程,是吗? Split将字符串拆分为数组,而不是文件到行。

open INPUT,"<my_file.txt" or die "no such file";
my @array1;
my @array2;
my $ref = \@array1;
while ( my $line = <INPUT> ) {
  chomp($line);
  if ( $line eq ".endm" ) {
    $ref = \@array2;
  } else {
    push(@$ref,$line);
  }
}
print $_ . " " foreach @array1;
print "\n";
print $_ . " " foreach @array2;