The "best" way depends on many factors and frankly speaking in your case I don't think it matters much. The passwd file is not too big. I am not going to code it for you I'll give you some hints instead.
You’re not really clear on the type of user interface needed. I’ll assume the command line will be fine.
The Unix passwd file is structured, it consists of records, separated by a colon. I assume a unique key for each record in the passwd file, i.e. the first column.
Apparently the user inputs a name for which you want to search the file?
- Open the file for reading
- Parse the file
Each line is a record, use for example
For each line:
@fields = split(/:/, $record);
to get the individual fields. Store the stuff in some data structure (e.g. Array of Hashes, Hash of Hashes).
- Close the file.
- Look for the fields you’re interested in, e.g. first name, second name.
- If it matches store the key in a list, say @matching_users
- After this you have a list with candidates, output the first 5 (or less) to the screen.
- Read the user input to select a user.
- Output the selected user to the screen.
You might want to add some error handling for incorrect user input.
You might want to combine/leave out steps to improve the solution.
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.