Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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
    [TStanley]: I'm sitting in the airport in Tampa, Florida at the moment, getting ready to head home.
    [talexb]: TStanley I'm flying to Vegas in two weeks time. After that flights leaving Vegas and Phoenix are late at night .. I've been hearing that it gets so hot that the air isn't dense enough for planes to take off. Weird. Guess you won't have that problem.
    [Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (8)
    As of 2017-06-23 14:15 GMT
    Find Nodes?
      Voting Booth?
      How many monitors do you use while coding?

      Results (548 votes). Check out past polls.