1 2 3 4 5
while ( my $line = <$filehandle> ) { if ( $line =~ m{START} .. $line =~ m{ENDE} } { # tu was mit Zeilen von START bis ENDE (Regex Matching) } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl -w
# Version 0.2
########################################
# Modules
use strict; # parameter Definition before use them
#######################################
# Paramet Definition
my $ct=1; # counter parameter
my $lines;
#my $target ="./TestFile.log";
#my $file2 ="./SpyLogs.log";
my ($file1,$file2,$NoString,$Strg);
my $BString ="Start";
my $EString ="End";
my @data =("");
###########################
# main programm
system("clear");
###########################
print "Which File: ";
$file1=<STDIN>;
chomp $file1;
print "File-Name for Result: ";
$file2=<STDIN>;
chomp $file2;
print "Call-Number: ";
$Strg=<STDIN>;
chomp $Strg;
$NoString ="Number=$Strg";
#print $NoString;
open(InFile,"<",$file1) or die $!; # Get the file to the Script Input
open(OutFile,">",$file2) or die $!; # Get the file to the Script Output
while ($lines = <InFile>)
{
if ( $lines =~ m{$BString} .. $lines =~ m{$EString} ) {
if ($lines =~ m{$NoString}){
push(@data,$lines);} # tu was mit Zeilen von START bis ENDE (Regex Matching)
}
}
print OutFile @data; # Store information to File
close(InFile);close(OutFile); # close the files
print OutFile @data if (join('', @data) =~ /das_muss_auch_drin_vorkommen/);
print OutFile @data if (@data ~~ /das_muss_auch_drin_vorkommen/);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use strict;
use warnings;
my $file = do {local $/; <DATA>};
if ($file =~ m/START\n(.*?irgendwas.*?)\nEND/s) {
print "Gefunden: Abschnitt ist:\n---\n$1\n---\n";
}
__DATA__
blubb
START
df
sgreg
irgendwas
dfg
END
sgdg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# ... my $flag = 0; while ($line = <InFile>) { if ( $line =~ m{$BString} .. $line =~ m{$EString} ) { # Merker setzen, ob bestimmter String im Block vorkommt $flag = 1 if $line =~ m{$NoString}; push @data, $line; # Merker ist gesetzt und Blockende ist erreicht if ( $flag == 1 && $line =~ m{$EString} ) { $flag = 0; # tu was extra mit @data } } } # ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# ... my $flag = 0; while (my $line = <InFile>) { if ( $line =~ m{$BString} .. $line =~ m{$EString} ) { # Merker setzen, ob bestimmter String im Block vorkommt $flag = 1 if $line =~ m{$NoString}; push @data, $line; } else { # Blockende ist erreicht if ( $flag == 1 ) { # Merker ist gesetzt # tu was extra mit @data # ... $flag = 0; } @data = (); } } # ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Start
foo
Number=1
End
Start
Fiete=1
End
Start
Number=2
End
Start
Number=3
End
Start
bar
Number=1
End
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#!/usr/bin/perl use strict; # parameter Definition before use them use warnings; # Version 0.3 my $BString = "Start"; my $EString = 'End'; sub ask_user { my $question = shift; print $question, ": "; chomp( my $answer = <STDIN> ); return $answer; } #> main #> ------------------------------------------------------------------------- system("clear"); my $infile = ask_user( "Please name the input file" ); my $outfile = ask_user( "Please name the output file" ); my $number = ask_user( "Please enter the call number" ); open my $in, '<', $infile or die "open($infile, ro) failed: $!\n"; open my $out, '>', $outfile or die "open($outfile, w) failed: $!\n"; my @alldata; my @data; my $flag = 0; while ( my $line = <$in> ) { if ( $line =~ m/$BString/ .. $line =~ m{$EString} ) { # speichere Zeilen von $Bstring bis $EString push @data, $line; $flag = 1 if $line =~ m{Number=$number}; if ( $line =~ m{$EString} ) { push @alldata, @data; if ( $flag == 1 ) { print $out @data; $flag = 0; } @data = (); } } } close $out; close $in;
1 2 3 4 5 6 7 8 9 10
my $number; while ( my $line = <$filehandle> ) { if ( $line =~ m{START Number=(\d+)} .. $line =~ m{ENDE} } { $number = $1 if $1; if ($number == 3151) ... else ... } }