[aklug] Re: perl lists question, sorting, sort of.

From: Arthur Corliss <acorliss@nevaeh-linux.org>
Date: Tue Aug 17 2010 - 13:03:52 AKDT

On Tue, 17 Aug 2010, Lee wrote:

> I'm consolidating and cleaning up some /etc/hosts here at work, and using it as a perl
> learning experience (rather than just doing it by hand).
>
> A short example of what I've got is:
>
> 172.17.21.40 mlpowks1c
> 172.17.21.40 mlpowks1d
> 172.17.21.41 mlpowks2
> 172.17.21.42 mlpowks3
> 172.17.21.43 mlpowks4
> 172.17.21.40 mlpowks1a
> 172.17.21.44 mlpowks5
> 172.17.21.46 mlpswks1
> 172.17.21.47 mlpswks2
> 172.17.21.46 drafts
> 172.17.21.48 mlpswks3
> 172.17.21.40 sat-ops
>
> Notice there are 4 names for 172.17.21.40 and 2 names for 172.17.21.46. As you may
> guess, there can be several lines in a hosts file for any given ip.
>
> What I'd like to do is consolidate this so it looks something like:
>
> 172.17.21.40 mlpowks1c mlpowks1d mlpowks1a sat-ops
> 172.17.21.41 mlpowks2
> 172.17.21.42 mlpowks3
> 172.17.21.43 mlpowks4
> 172.17.21.44 mlpowks5
> 172.17.21.46 mlpswks1 drafts
> 172.17.21.47 mlpswks2
> 172.17.21.48 mlpswks3
>
> Apparently, from what I read, doing the hash thing will simply take the last element,
> since the 'key' (that is the ip) is exclusively indexed. Testing seems to bear this out.
>
> I can also brute force it was a lot of loops and conditionals. That's an option.
>
> But, before I do that, is there an easy, 'perl-y' way to do this?

   perl -ane '$ip{$F[0]} .= " $F[1]"; END { foreach (keys %ip) {
     print "$_ $ip{$_}\n" }; };' /etc/hosts

         --Arthur Corliss
           Live Free or Die
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Tue Aug 17 13:04:03 2010

This archive was generated by hypermail 2.1.8 : Tue Aug 17 2010 - 13:04:03 AKDT