Scripting question

Roland Smith rsmith at xs4all.nl
Thu Sep 13 11:35:07 PDT 2007


On Thu, Sep 13, 2007 at 10:16:40AM -0700, Kurt Buff wrote:
> I'm trying to do some text file manipulation, and it's driving me nuts.
> 
> I've got a sorted file of SMTP addresses, and want to eliminate the
> lines that are the same up to a space character within the line.
> 
> Example:
> 
> kurt.buff at gmail.com NO
> kurt.buff at gmail.com OK
> 
> The above lines *both* need to be eliminated from output - I don't
> want the first or second of them, I want them both gone.
> 
> I've looked at sort and uniq, and I've googled a fair bit but can't
> seem to find anything that would do this.
> 
> I don't have the perl skills, though that would be ideal.
> 
> Any help out there?

#!/usr/bin/perl
while (<>) {
    # Assuming no whitespace in addresses; kill everything after the first space
    s/ .*$//;
    # Store the name & count in a hash
    $names{$_}++;
}
# Go over the hash
while (($name,$count) = each(%names)) {
  if ($count == 1) {
      # print unique names.
      print $name, "\n";
  }
}


Roland
-- 
R.F.Smith                                   http://www.xs4all.nl/~rsmith/
[plain text _non-HTML_ PGP/GnuPG encrypted/signed email much appreciated]
pgp: 1A2B 477F 9970 BA3C 2914  B7CE 1277 EFB0 C321 A725 (KeyID: C321A725)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20070913/c0a55b26/attachment-0001.pgp


More information about the freebsd-questions mailing list