2016-04-22 8 views
-1

Ich habe eine Datei, die Zeichenfolge wie folgt enthalten:Wie kann ich geteilt String pro Zeile auf Datei

- ' *[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 

Ich möchte diese Zeichenfolge pro Zeile aufzuteilen 2 Teil zu sein, und ich hoffe, dass das Ergebnis wie folgt aus:

- 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 

und der Code versuche ich habe, ist, wie folgt aus:

#!/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++; 
} 

ich bin verwirrt, weil ich nicht die Zeichenfolge mit „“ Schlitz kann (SPAC e), weil jede Zeile einen anderen Ordnungsraum hat und ich nicht 2-teilig werden kann. Danke.

+0

nicht gespalten dafür verwenden, einen regulären Ausdruck verwenden. http://perldoc.perl.org/perlre.html – xxfelixxx

+0

https://regex101.com/ – xxfelixxx

+0

Bitte bearbeiten Sie Ihren Beitrag mit klaren Beispiel und klare Ausgabe, was Sie genau wollen. – mkHun

Antwort

0
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; 

Ausgang:

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; 

, um Ihren Code Angewandt es in etwa so aussehen könnte:

while ($i<5) 
{ 

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

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

    $i++; 
} 
Verwandte Themen