Look for Certain Number in a Field and Retrieve Line
Keywords: LAFFDB binary indexing
Question:
Presently, I have a tab delimited file (Security log dumpel file) that I look for a curtain number in the 5th field (event), then retrieve the entire line. Now, I would like to load this file into a memory buffer, search for this event (this 3 digit number can also exist in other fields, so I need to be able to search in the 5th field only) and then retrieve the entire line into a variable. Any suggestion?Answer:
Well, I would SUGGEST taking a look at the LAFFDB extender. What you really have there is a Light ascii flat file database. that's exactly what the extender is made for.Example of winding loop - dbfindrecord returns a -1 when there's no more matches
- You'll start by opening the database with DBOPEN().
- Next, set up some variables and use DBBINDCOL() to link them to the fields in the records of the database.
- Then, use DBFINDRECORD() to search for the ones you want. you can set up a while loop to "wind" through the matching records pretty easily.
- Each time you find a match, just use DBGETENTIRERECORD() and the variables will be filled with the values you want, then you can do your actual work.
while @true linenumber = dbfindrecord(...search criteria...) if linenumber = -1 then break dbgetentirerecord(...details of the record...) gosub Deal_with_the_record endwhile
Here is a more useful example:Addextender("LAFFD34i.DLL") db_fn = "Security log dump file.log" db_create = 0 ; file should exist db_model = 2 ; traditional db_cols = 0 ; use the number of columns in the first line of the file db_format = 0 ; delimiter based db_delimiter = @tab db_options = "" ; don't bother hnd_db = dbopen(db_fn,db_create,db_model,db_cols,db_format,db_delimit,db_option) db_colcount = dbgetcolumncount(hnd_db) for a = 1 to db_colcount dbbindcol(hnd_db,a,VAR%a%) next search_flags = 1 + 4 ; use and return fixed record nums (note that fixed record nums are 1 based), and search forward search_col = 5 ; this is the column you wanted to skim search_value = curtain_number ; this is what you wanted to search for search_case = 0 ; not case sensitive search_start = 0 ; start at zero to actually search record #1 get_flags = 1 ; get a fixed record num (needs to match search flag) linenumber = search_start ; need to init the variable while @true linenumber = dbfindrecord(hnd_db,linenumber,search_flags,search_col,search_value,search_case)) ;returns -1 if no more matches if linenumber < 0 then break ; breaks on done searching OR error (because error is -42424242) dbgetentirerecord(hnd_db,linenumber,get_flags) gosub Deal_with_the_record endwhile dbclose(hnd_db) exit ;--------------------------------------------------------------------------------------------------------- :Deal_with_the_record ; here is where you will do whatever you want.. ; the variables will be named var1, var2, var3, var4... etc ; this routine will run once for each matching line in your file ; have at it... return ;---------------------------------------------------------------------------------------------------------
Article ID: W15056