×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Last match from a file

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.

RE: Last match from a file

Hi

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

my $last; # declare it before the loop
while (my $cline = <$hadlerdirfile_fh>)
{
    chomp $cline;

    if ($cline =~ /End lot/)
    {
        $last = $cline;
    }
}
print "$last \n"; # print it after the loop 

Feherke.
feherke.github.io

RE: Last match from a file

(OP)
Thank you!

RE: Last match from a file

(OP)
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.

RE: Last match from a file

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 ?

RE: Last match from a file

(OP)
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

RE: Last match from a file

Isn't it similar as you try to achieve in this python thread ?
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

* 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 ?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close