Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I get the last occurrence of a word (or words) in a file?

by Anonymous Monk
on May 11, 2000 at 01:03 UTC ( [id://11056]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a list of servers with their status in a file. I need to get the last occurence of each of the servers in the file. The server names are VISHU, VISHU2, JITHU, JITHU2, MADHU, MADHU2
The file format is :
VISHU: server running 12/3/99
VISHU2: server down 12/3/99
VISHU :server running 3/12/00
JITHU : server down 3/12/00
JITHU2: server running 4/12/00
......

I need to find the last occurences of each of the servers.

Originally posted as a Categorized Question.

  • Comment on How do I get the last occurrence of a word (or words) in a file?

Replies are listed 'Best First'.
Re: How do I get the last occurrance of a word (or words) in a file?
by merlyn (Sage) on May 11, 2000 at 02:13 UTC
    By getting the "last" occurrance, I don't know if you want to know the line it's on (as a line number), or the contents of the line. So I'll do both:
    while (<>) { if /^((VIS|JIT|MAD)HU2?):/) { $found{$1} = [$., $_]; } } for (sort keys %found) { print "$_ => @{$found{$_}}\n"; }
    season to taste.
Re: How do I get the last occurrence of a word (or words) in a file?
by mslattery (Initiate) on Apr 19, 2002 at 18:50 UTC
    Or stated another way ... arn't hashes great?

    while (<>) { /^(.+)\:/ ; # Capture Server Name $hash{$1} = $_ ; # Create Hash Key if found } foreach $server (sort keys %hash) { print "$hash{$server}\n" ; }
    Just for another style!

    Have fun,
    mslattery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11056]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 19:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found