Retrieve Specific Lines from a File

Author: Ashley Truter
Return to REBOL Cookbook

This example will show you how to retrieve specific lines directly from a file. This can be useful when you have large files or memory is at a premium.

First, for the examples below, we need to create a sample file with three lines of text:

    write/lines %data.txt ["one" "two" "three"]

Ordinarily, we would load this file as lines into memory with:

    data: read/lines %data.txt

and then access it like an array:

    print data/1
    print first data

What we want to do in this example, however, is read lines from the file without having to load it into memory. REBOL lets us access a file directly by way of a port. We can open and close our sample file with the following code:

    file: open/lines/direct %data.txt
    close file

Using SKIP we can now write a simple seek function:

    print-seek: function [fname [file!] pos [integer!]] [file] [
        file: open/lines/direct fname
        print first skip file pos - 1
        close file

    print-seek %data.txt 2

The magic occurs with

    print first skip file pos - 1

which is just saying, "Print the first value of the remaining values in the file, *after* skipping ahead a number of lines less one (for the line I am interested in)".

With just a few more lines of code we can retrieve more than one line at a time with:

    print-seek: function [fname [file!] block [block!]] [file offset] [
        offset: 1
        file: open/lines/direct fname
        foreach pos block [
            file: skip file (pos - offset)
            offset: pos + 1
            print first file
        close file

    print-seek %data.txt [1 3]

which is doing what our first example did but "remembering" where we are in the file.

This function can obviously be improved in a number of ways. What happens when a non-integer is provided as input? A number smaller or larger than the number of lines? What happens if the numbers are not provided in ascending order or the provided file name does not exist?

2006 REBOL Technologies REBOL.com REBOL.net