Last match from a file
Last match from a file
(OP)
Hi, need help again.
I have a file and it can have one line or many lines with the words I'm trying to match
Words to match:
"... Lot End..."
I need to capture the last line that has this occurrence.
I thought I could just push all the match lines to an array and then get the last element but somehow it does not work.
Here is a script I'm trying:
while (my $cline = <$hadlerdirfile_fh>)
{
chomp $cline ;
if ($cline =~/End lot/g )
{
push @three_clines, $cline;
my $last = pop@three_clines ;
print "$last \n";
}
}
Thank you.
I have a file and it can have one line or many lines with the words I'm trying to match
Words to match:
"... Lot End..."
I need to capture the last line that has this occurrence.
I thought I could just push all the match lines to an array and then get the last element but somehow it does not work.
Here is a script I'm trying:
while (my $cline = <$hadlerdirfile_fh>)
{
chomp $cline ;
if ($cline =~/End lot/g )
{
push @three_clines, $cline;
my $last = pop@three_clines ;
print "$last \n";
}
}
Thank you.
RE: Last match from a file
There you push and pop one after another, that is nothing but a more complicated my $last = $cline; assignment each time a match was found. Beside that, having the declaration of $last inside the if's block is probably bad idea, as will not be available outside.
CODE --> Perl
Feherke.
feherke.github.io
RE: Last match from a file
RE: Last match from a file
I need to pull out lines that have a “Lot start” line and an “End Lot” line, but I need only one match of an “End Lot” line (preferably last match).
The file goes something like this :
Some lines here Lot Started some lines here
More lines
More lines
More lines
More lines End Lot some other files
More lines End Lot some other files
More lines End Lot some other files
Some lines here Lot Started some lines here
More lines
More lines
More lines
More lines End Lot some other files
More lines End Lot some other files
Some more lines here Lot Started some lines here
More lines
More lines
More lines
More lines End Lot some other files
More lines End Lot some other files
More lines End Lot some other files
More lines End Lot some other files
I need an output look like this:
Lot Started, End Lot
Lot Started, End Lot
Lot Started, End Lot
Lot Started, End Lot
Thank you.
RE: Last match from a file
For example, if you have this input
CODE
Is the output you need like this ?
CODE
CODE
RE: Last match from a file
L
RE: Last match from a file
https://www.tek-tips.com/viewthread.cfm?qid=180245...
With the example I posted in the python thread, I got on the data I posted here
on 23 Apr 20 14:40 this output:
CODE
Isn't it the same what you need ?