walkingthecow has asked for the wisdom of the Perl Monks concerning the following question:

Hey all,

This is not a CGI/perl related question, and the option of an SQL database is out for me. I am trying to figure out the best way to open an /etc/passwd file with thousands of users and compare it to a flat-file database that I created. I have some examples below:

FROM PASSWD FILE:
chamnsa:71807168:4653:Alex A. Chamn,GROUP:/users/group/chapmanr:/usr/bin/ksh
clarkj:506558:4653:Janet Clark,WHEEL,,ED74561:/users/group/clarkj:/usr/bin/ksh
n647x12:8792:4653:Jim.E.Dew,SYSTEM,123456,ED74493:/users/system/n647x12:/usr/bin/ksh
chas:71807168:4653:Alex Chamn,GROUP:/users/group/chapmanr:/usr/bin/ksh


I want to use names, and possibly usernames to search a file that looks like this:
chamnsa:Alex:A:Chamn:00000432543:ED98709
n47x12:James:E:Dew:0000012345:ED98765
chas:Alex:D:Chamn:00000765987:ED12345
clarkj:Janet:C:Clark:00000567432:ED09876


Basically from here I would like for it to match Alex Chamn for both Alex Chamns, each with a different middle initial, and then print it out like so:
1. chamnsa:Alex:A:Chamn:00000432543:ED98709
2. chas:Alex:D:Chamn:00000765987:ED12345
If the user selects 1, then the information is then written to an output file like so:

chamnsa:Alex A. Chamn,GROUP,00000432543,ED98709


The group is actually pulled from the system being worked on, so each field from database has to be saved and accessed from an array.

Basically, I want it to search a name (from /etc/passwd) in a database and pull up to five people and display them, then allowing user which one to use. I only want it to display up to 5 results. So, for example, if I have Bill Johnson from /etc/passwd and there are 50 Bill Johnsons in the database, I only want to display the first five.

Anyway, just wondering the best way to do this...

Replies are listed 'Best First'.
Re: Best way to search flat-file database
by dHarry (Abbot) on Aug 04, 2008 at 09:35 UTC

    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.

Re: Best way to search flat-file database
by Anonymous Monk on Aug 04, 2008 at 07:41 UTC
    and the option of an SQL database is out for me

    No it isn't :) if you use a database-in-a-file, like DBD::SQLite, or DBD::AnyData

      I can only use core modules, and perl version is 5.003. Yes, I know, please no lectures though...
Re: Best way to search flat-file database
by Illuminatus (Curate) on Aug 04, 2008 at 10:06 UTC
    Have you looked at String::Approx? If you can split the name into tokens, this would probably suffice. You might have to be creative if you have a case where names run together: Cowboy P Neal cowboyneal
    It still could probably be made to work even in this case.