in reply to parsing a terrible /etc/hosts

G'day f77coder,

[Please put your data, as well as your code, within <code>...</code> tags. Parts of your data have been rendered as links due to the presence of square brackets. See "Writeup Formatting Tips" for more on this.]

"so i want to clean the old file by removing comments, and duplicates, so far i have"

Here's some comments on what you have "so far":

Here's how I might have approached this. I've added some additional test data.

#!/usr/bin/env perl -l use strict; use warnings; my (%data, %seen, @order); while (<DATA>) { s/\s*#.*$//; next if /^\s*$/; my ($key, @rest) = split; push @order, $key unless $seen{$key}++; push @{$data{$key}}, grep { ! $seen{$_}++ } @rest; } print join ' ', $_, @{$data{$_}} for @order; __DATA__ 127.0.0.1 c2.gostats.com #SpySweeper.Spy.Cookie 127.0.0.1 ads.goyk.com # 1-800-hostingAS3321069.41.160.0 - 69.41.191.255 127.0.0.1 2a02:598:2::1095 127.0.0.2 2a02:598:2::1096 127.0.0.2 2a02:598:2::1096 127.0.0.2 2a02:598:2::1096 127.0.0.2

Output:

127.0.0.1 c2.gostats.com ads.goyk.com 2a02:598:2::1095 127.0.0.2 2a02:598:2::1096

— Ken

Replies are listed 'Best First'.
Re^2: parsing a terrible /etc/hosts
by f77coder (Beadle) on Mar 25, 2017 at 02:26 UTC
    Hi Ken, Thanks for the help and critic.