【问题标题】:How can i split string per line on file如何在文件中每行拆分字符串
【发布时间】:2016-08-15 13:05:17
【问题描述】:

我有一个包含如下字符串的文件:

- ' *[0-9]-? [^a-c]@[*-^a-c]' '' < temp-test/758.inp.325.1
- ' *[0-9]-? [^a-c]@[*-^a-c]' '' < temp-test/759.inp.325.3
- ' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1133.inp.487.1`enter code here`
- ' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1134.inp.487.2
- '"@@' 'm' < input/ruin.1890

我想把这个字符串每行分成两部分,我希望结果是这样的:

- line 1: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/758.inp.325.1
- line 2: array[0]=' *[0-9]-? [^a-c]@[*-^a-c]'; array [1]='' < temp-test/759.inp.325.3
- line 3: array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1133.inp.487.1
- line 4: array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]'; array[1]='NEW' < temp-test/1134.inp.487.2
- line 5: array[0]='"@@'; array[1]='m' < input/ruin.1890

我尝试过的代码是这样的:

#!/usr/bin/perl

# location of universe file
$tc = "/root/Desktop/SIEMENS/replace/testplans.alt/universe";

# open file universe;
open( F, "<$tc" );
@test_case = <F>;

while ( $i < 5 ) {

    $test_case[$i] =~ s/ //;
    @isi = split( / /, $test_case[$i] );

    if ( $#isi == 2 ) {
        print "Input1:" . $isi[0] . "\n";
        print "Input2:" . $isi[1] . "\n";
        print "Input3:" . $isi[2] . "\n";
    }

    $i++;
}

我很困惑,因为我不能用“”(空格)分割那个字符串,因为每一行都有不同的顺序空间,我不能成为 2 部分。 谢谢。

【问题讨论】:

  • 不要为此使用拆分,使用正则表达式。 perldoc.perl.org/perlre.html
  • 请用清晰的例子编辑你的帖子,并清楚地输出你想要的。
  • 目前还不清楚您的输入文件中的内容和所需的输出。请准确复制数据并像使用 Perl 代码一样使用 Ctrl-K 突出显示它

标签: arrays perl file split


【解决方案1】:
use strict;
use warnings;

# this stuff just gives me your data for testing

my $data =<<EOF;
' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/758.inp.325.1
' [0-9]-? [^a-c]\@[-^a-c]' '' < temp-test/759.inp.325.3
' *[0-9]\@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1133.inp.487.1
' *[0-9]\@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' 'NEW' < temp-test/1134.inp.487.2
'"\@\@' 'm' < input/ruin.1890
EOF

for my $line (split(/\n/,$data))
{

    # this re splits your strings according
    # to what I perceive to be your requirements

    if ($line =~ /^(.*)('.*?' <.*)$/)
    {
        print("array[0]=$1; array[1]=$2;\n")
    }
}


1;

输出:

array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/758.inp.325.1;
array[0]=' [0-9]-? [^a-c]@[-^a-c]' ; array[1]='' < temp-test/759.inp.325.3;
array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1133.inp.487.1
array[0]=' *[0-9]@[[9-B]??[0-9]-[^-[^0-9]-[a-c][^a-c]' ; array[1]='NEW' < temp-test/1134.inp.487.2;
array[0]='"@@' ; array[1]='m' < input/ruin.1890;

应用于您的代码,它可能看起来像这样:

while ($i<5)
{

    $test_case[$i] =~ /^(.*)('.*?' <.*)$/;
    @isi = ($1,$2);

    print "Input1:".$isi[0]."\n";
    print "Input2:".$isi[1]."\n";

    $i++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多