Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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 ( #11056=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on May 11, 2000 at 01:03 UTC
Q&A  > regular expressions


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.

Answer: How do I get the last occurrance of a word (or words) in a file?
contributed by merlyn

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.
Answer: How do I get the last occurrence of a word (or words) in a file?
contributed by mslattery

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,

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and nobody stirs...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (4)
    As of 2018-07-20 07:18 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (426 votes). Check out past polls.