Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

identify unique records

Status
Not open for further replies.

tempest92

Technical User
Sep 16, 2003
110
0
0
GB
hi,

can someone help please. i have a file in the following format -

serverName:username:uid:pgrp

e.g.

s1:bob:999:staff
s2:bob:888:staff
s3:bob:999:staff
s1:jon:777:staff
s2:jon:666:staff
s3:jon:666:staff

so i would like to read through this file and for the records that there is only 1 of i.e. jon:777 but there are other jon:xxx records in the file, write the jon:777 record to a file.

thanks
 
This really requires a higher language than Unix scripting. Personally I'd do it in perl, I'm sure there are some who would do it in awk.

The (untested) perl version would go something like
Code:
my %users;
open FH, "datafile";
while (<FH>)
  {
  my ( $server, $username, $uid, $group ) = split /:/;
  exists $users{$username} and push @$users{$username}, "$serfer:$username:$uid:$group" or $users{$username} = [ "$serfer:$username:$uid:$group" ];
  }
close FH;
foreach my $user ( keys %users )
  {
  $#{@$users{$user}} or next;
  foreach ( @$users{$user} )
    { print "$_\n"; }
  }


Ceci n'est pas une signature
Columb Healy
 
This will work... to an extent

Code:
sort -t: -k2 /some/file|uniq -f 1 -u > /some/newfile

- the input file is sorted on the 2nd field (username)
- the sorted output is passed to uniq, which will skip the
first field (server) and spit out only the lines that
are not repeated. IE s1:jon:777:staff

The catch here is that if the pgrp changes, IE its something other than staff, then uniq doesnt generate the desired output.

As columb said, you really want to use awk/perl/other higher level language to process this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top