DEV Community

Noboru Saito
Noboru Saito

Posted on

View large files and CSV files using ov

Introduction

ov is a terminal pager.
It supports only UTF-8.

I introduce it because ov is also suitable for opening large files as a pager.

Earlier versions of ov used to read all files into memory, but since v0.30.0, it uses seek to read only what it needs.
Therefore, large files and CSV files can now be viewed quickly.

Not only is it faster to open, but it is also as comfortable as possible to operate after opening.

Comparison with less

While less can open even large files quickly, there is still some inactivity time after opening because it reads the entire file when it needs to.

ov does the same and reads the file in a thread (goroutine) after it is opened, thus not interfering with the operation.

Below is a comparison of the operation of a 4.6 GB file opened with less and ov.
The file is opened with line numbers.
With less it takes time to calculate the line numbers, but with ov it moves to the end and then back to the beginning again.

large-file

Although ov continues to read the file in goroutine when it is opened, it does not actually put the entire contents of the file into memory, but records the number of lines and the location of the file to seek and then reads it down.
This is done in blocks of 10,000 lines. Since it reads in blocks as it needs to, it uses less memory and can move faster.

Limitations

The above ov behavior requires that the file be seek able; pipe input and special files that cannot be seek are loaded into memory and acted upon.
(It is also possible to limit the memory, so that content larger than memory will stop reading in the middle of the file or produce a part of the file that cannot be returned if you go ahead).
Also, ov can open compressed files without being aware of it, but compressed files behave like piped input because seek and decompressed bytes do not match.

Displaying CSV files

Since ov has a history of originally adding features to make it convenient for displaying psql, it is also convenient for displaying CSV files.

Fixed header display

ov allows fixed display of headers.
The --header option displays a fixed header.

If the first line in a CSV file is a header, you can use the following.

$ ov --header 1 file.csv
Enter fullscreen mode Exit fullscreen mode

The latest less can also display fixed headers, but the difference is that ov supports header wrapping.

column mode

In ov, use --column-delimiter to specify the delimiter ("," is the default, so CSV can be omitted) and the --column-mode option to display in column mode.

$ ov --column-delimiter "," --column-mode file.csv
Enter fullscreen mode Exit fullscreen mode

In column mode, columns can be highlighted, making it easier to locate columns in CSV files that are not aligned vertically.

In addition, --column-rainbow will display each column in a different color.
These options can be combined as follows.

$ ov --column-delimiter "," --column-mode --column-rainbow file.csv
Enter fullscreen mode Exit fullscreen mode

ov-csv
One limitation is that it does not parse as a CSV file, so columns containing delimiters (",") or newlines will not be displayed correctly.
Conversely, because it does not parse as a CSV, even large CSV files can be displayed and moved at high speed.

Top comments (0)