I need help with perl code. I have a data file with lots of data
example: data.txt file
START
DATA1 sjdfh kjhdf
DATA2 sdkjfhk jds dshfgdf
... Around 20 - 30 lines
END
LDDDD awdkjasd a sdkahgdk jasdh
SOME CRAP
Some EMPTY lines etc
START
DATA1 sjdfh kjhdf
DATA2 sdkjfhk jds dshfgdf
... Around 20 - 30 lines
END
SOME EMPTY Lines
SOME MORE CRAP
START
DATA1 sjdfh kjhdf
DATA2 sdkjfhk jds dshfgdf
STOP
START
DATA1 sjdfh kjhdf
DATA2 sdkjfhk jds dshfgdf
END
Now in Data file I need to extract any line witH START and end with END or STOP
when I use /START/../END/ I can extract files but it does not have all the values I need .
Also I am using
$* # Enable multi line pattern etc.
Any quick snippet will help . Thanks for reading the post.
def123
2
This will save all the lines which starts with START and ends with END and STOP to another file.
#!/usr/bin/perl
$file ="test.txt";
open(INFILE,$file);
$i=0;
while (<INFILE>){
push (@arr1,split(/\n/,$_));
}
for ($i=0;$i<$#arr1;$i++) {
$myline = $arr1[$i];
$j=$i;
if ($myline =~/START/i) {
until(($myline =~/END/) || ($myline =~/STOP/)) {
$myline = $arr1[$j];
push(@arr,$myline);
$j=$j+1;
}
}
}
close INFILE;
$file1="log.txt";
open(OUTFILE,">",$file1);
foreach $val(@arr) {
print OUTFILE $val."\n";
}
close OUTFILE;
Thank you SIR def123 This does what I need .. Thanks
Only thing if I have START also mutiple start then
example
START and START ONE or ANOTHER ONE etc ...
I am playing around with
if (($myline =~ /START/ ) || ( ...
I guess it is correct ...
Thank you again for your code