![]() |
|
![]() | ||
|
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... | |
| |
| |
|
![]() | ||
|
There is no answer at this time. |
![]() | ||
|
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; } |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |