I need to verify there is no duplicate UIDs in the /etc/passwd file. My plan was to sort the file by the UID, and then check for lines with UID = to previous line.
Is this the best way?
I was trying this code:
#!/usr/bin/perl -w
#<SortPW2.pl> /etc/passwd sort by UID
$file="/etc/passwd";
my @array = do { local( @ARGV, $/ ) = $file ; <> } ;
#my @array = (
#'555900:user:lee:0:23030',
#'3520916:user:joe:0:20487',
#'395284:user:richard:3:17557',
#'807060:group:smith:0:20573',
#'381940:home:kimble:1:625',
#);
my @sorted = map {$_->[0]}
sort {$a->[1] <=> $b->[1] || $a->[1] cmp $b->[1]}
map {[$_,(split':')[0]]} @array;
print "$_\n" for @sorted;
The commented out code works, but the modification to source the array from /etc/passwd fails.
Is it the line feeds? how would I remove them?
Or is there a better way.
All help appreciated. Thanx
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.