in reply to Best way to search flat-file database

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?

  1. Open the file for reading
  2. 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).

  3. Close the file.
  4. Look for the fields you’re interested in, e.g. first name, second name.
  5. If it matches store the key in a list, say @matching_users
  6. After this you have a list with candidates, output the first 5 (or less) to the screen.
  7. Read the user input to select a user.
  8. 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.

  • Comment on Re: Best way to search flat-file database