Well, if you're pulling rows out of a database, you need two queries: one of them just counts the number of rows in the entire search. This lets you calculate how many pages of results there are. The second query just selects the subset of that result you need. Typically, you'll need a LIMIT and/or OFFSET clauses for this.
If you're just searching through text files in your filesystem, the approach should be the same -- it's much less efficient to store the results of an entire search in memory when you're only really going to work with a couple of them.