Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last match from a file

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
US
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:
[sup] while (my $cline = <$hadlerdirfile_fh>)
{
chomp $cline ;

if ($cline =~/End lot/g )
{
push @three_clines, $cline;
my $last = pop@three_clines ;
print "$last \n";
}
}[/sup]
Thank you.
 
Hi

There you [tt]push[/tt] and [tt]pop[/tt] one after another, that is nothing but a more complicated [tt]my [navy]$last[/navy] [teal]=[/teal] [navy]$cline[/navy][teal];[/teal][/tt] assignment each time a match was found. Beside that, having the declaration of $last inside the [tt]if[/tt]'s block is probably bad idea, as will not be available outside.

Perl:
[b]my[/b] [navy]$last[/navy][teal];[/teal] [gray]# declare it before the loop[/gray]
[b]while[/b] [teal]([/teal][b]my[/b] [navy]$cline[/navy] [teal]=[/teal] [i][green]<$hadlerdirfile_fh>[/green][/i][teal])[/teal]
[teal]{[/teal]
    [b]chomp[/b] [navy]$cline[/navy][teal];[/teal]

    [b]if[/b] [teal]([/teal][navy]$cline[/navy] [teal]=~[/teal] [i][green]/End lot/[/green][/i][teal])[/teal]
    [teal]{[/teal]
        [navy]$last[/navy] [teal]=[/teal] [navy]$cline[/navy][teal];[/teal]
    [teal]}[/teal]
[teal]}[/teal]
[b]print[/b] [i][green]"$last \n"[/green][/i][teal];[/teal] [gray]# print it after the loop[/gray]

Feherke.
feherke.github.io
 
Hi, I’m still having problems with the last matched lines.
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.
 
I'm not sure, if I understand what you want to do:

For example, if you have this input
Code:
line 01
line 02 .. Lot Started ..
line 03
line 04 .. End Lot ..
...
line 10 .. End Lot ..
line 15 .. End Lot ..
...
line 20 .. Lot Started ..
line 21
line 22 .. Lot Started ..
...
line 25 .. End Lot ..
line 26
...
line 30 .. End Lot ..
line 31
...
line 39
line 40 .. Lot Started ..
...

Is the output you need like this ?
Code:
line 02 .. Lot Started ..  line 15 .. End Lot ..
line 20 .. Lot Started ..  line 30 .. End Lot ..
..
Or should be the output like this ?
Code:
line 02 .. Lot Started ..  line 15 .. End Lot ..
line 22 .. Lot Started ..  line 30 .. End Lot ..
..
Or what else ?
 
For each Lot Started I'd like to print one End Lot (from the block of the End Lot) preferably last one (End Lot).
L
 
Isn't it similar as you try to achieve in this python thread ?
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:
* line pair found:
line 02 .. Lot Started ..
line 15 .. End Lot ..
* line pair found:
line 22 .. Lot Started ..
line 30 .. End Lot ..

Isn't it the same what you need ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top