Google Answers Logo
View Question
 
Q: perl script help ( No Answer,   2 Comments )
Question  
Subject: perl script help
Category: Computers > Programming
Asked by: kyra1234-ga
List Price: $19.00
Posted: 21 Aug 2002 16:07 PDT
Expires: 20 Sep 2002 16:07 PDT
Question ID: 57131
this must be accomplished using an old version of perl; I think it's
something like 5.0.  please keep that in mind when you answer.

I have a hash table of names, that all have the value of 0.  ie:

first1 last1
first2 last2
first3 last3

So, if one entered  print $hash{"first1 last1"}  the computer would
print 0.


I NEED a perl script that does the following:

Using the hash_table mentioned above, that's called hash:

extracts the /etc/passwd file and, using split, puts the fields of
each record into a normal (not hash table) array ( I tried to do this
with @array = split("\n", 'cat /etc/passwd'); and also with ($logon,
$passwd, $id, $group, $name, $home, $shell) = split(/:/); ) but it
didn't work...

Then, it must check each name in the 5th column of /etc/passwd (the
user name field) and for each name that it finds that matches a name
in the hash table , increment the corresponding number in the hash
table by one.  (remember, each name is already set to zero in the hash
table).  When the fifth column is empty, or the line is blank, this
part of the script must ignore that line.

So, basically, the script is counting how many duplicates are in the
/etc/passwd file, and incrementing the value of a hash table that
already has all of the matching names set to zero.  We are counting
dups in the /etc/passwd file.

Then, the script must display the total number of records (how many
lines are in the hash table).  Then it must display how many total
duplicates there were.  Then it must display which names were
duplicates, and how many duplicates each name had.  It would be nice
if the script was as simple as possible as well...

Clarification of Question by kyra1234-ga on 21 Aug 2002 19:34 PDT
lexi-ga,

Thanks for your response.  I'm sorry, but this must be done in perl. 
The version is 5.0xxx.  I know it's old, but I can't change that. 
Please let me know if you can help out.

Thanks,  Krya

Clarification of Question by kyra1234-ga on 23 Aug 2002 09:02 PDT
timkay-ga,

Thanks for your response - you deserve the credit for this because
your answer was very close to what I needed.  I got errors on both
print <<EOF lines, but maybe my perl version is too old?

How do I select your response as the correct 'answer' so that you will
be awarded the payment?  Or, do I have to do anything?

Thanks again.

Request for Question Clarification by studboy-ga on 23 Aug 2002 14:44 PDT
Perl is stupid--there's a space after EOF--take it out
and it will work like a charm.
Answer  
There is no answer at this time.

Comments  
Subject: Re: perl script help
From: lexi-ga on 21 Aug 2002 19:23 PDT
 
You don't need perl to count duplicate names in the /etc/password file
- this shell fragment will tell you which names are there more than
once:

cut -d':' -f5 /etc/passwd | sort | uniq -idc

(and if you want to know how many of those there were, pipe the final
output to 'wc -l').

If you need to do this with perl, you might want to specify more
completely the version number you have to work with - various 5.x
versions can behave very differently depending on what you're trying
to do.
Subject: Re: perl script help
From: timkay-ga on 21 Aug 2002 23:32 PDT
 
#!/usr/bin/perl

###
###  It doesn't matter that %hash is preset with zeros.  If you don't do it,
###  the non-existent entries will evaluate to undef, which will be treated
###  as 0 in a numeric context.
###

###
###  The script processes a file named 'passwd' in the current directory if
###  it exists.  Otherwise, it processes /etc/passwd.
###

@ARGV = -e 'passwd'? 'passwd': '/etc/passwd';

while (<>)
{   
    chop;
    next if /^$/;
    my($logon, $passwd, $id, $group, $name, $home, $shell) = split(/:/);
    next unless $name;
    $hash{$name}++;
}

my $rows = keys %hash;
my $dups = grep {$_ > 1} values %hash;

print <<EOF;
$rows total records
$dups duplicate records
EOF

print <<EOF if $dups;

count    name
-----   --------------------------
EOF

for (sort keys %hash)
{   
    printf "%4d\t$_\n", $hash{$_} if $hash{$_} > 1;
}

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy