line - extract lines from a file


line [ -nrv ] lines [ files ]
line [ -nrv ] -f fromline -t toline [ files ]


line extracts selected lines, specified by line number, from standard input or named files. Line numbers are 1-based.

The line numbers to be extracted may be specified in several ways. The most basic is to indicate individual line numbers individually, or line number ranges using the notation m-n. Multiple line numbers or ranges may be separated by commas, or entered as separate arguments. For example, the invocation

	line 1,5,10-20 inputfile
would print lines 1, 5, and 10 through 20 of the file named inputfile. An equivalent invocation would be
	line 1 5 10-20 inputfile

In the m-n notation, either m or n (but not both) may be omitted. The notation -n means from the beginning of the file up to line n. The notation m- means from line m to the end of the file.

Because multiple line numbers may be entered as separate arguments, there is an ambiguity if an input filename has a name which looks like a number. To resolve the ambiguity, use an alternative pathname for the file which does not begin with a digit. The simplest way to do so is to precede the numeric filename with ``./''.


Number output lines; precede printed lines with their line numbers (separated by a colon).
Random access. By default, all line numbers selected and printed must be monotonically increasing. With -r, it is permissible to ``back up'' in the input file to print some earlier lines preceding those already printed. To support this functionality, line builds an index of the lseek offsets of the lines it has seen. Random-access selection with -r is therefore possible only when the input is a true file, since seeking is impossible on pipes. Also, the line number index may consume large amounts of memory for large files.
Invert; print all lines not selected.
Print program's version number.
Print a brief help message.


It is also possible to specify line numbers or ranges using explicit option flags:

-f n
(``from'') Start printing at line n.
-t n
(``to'') Stop printing at line n.
-e n
(``end'') Stop printing just before line n, that is, at line n-1.
-p n
(``plus'') Print n lines past the last-specified start line. That is,
	-f m -p n
is equivalent to
	-f m -t m+n-1
(or to -f m -e m+n ).

Any number of -f, -t, -e, and -p options may appear. When these options are used, lone numbers on the command line are not interpreted as line numbers or ranges, and are therefore unambiguously usable as filenames.

The -f, -t, -e, and -p options are intended for use in shell scripts, when a complicated set of line number ranges is being built up on-the-fly, and when constructing a single string containing many dashes and commas would be even more cumbersome.


When line is invoked with multiple input filenames, the specified lines are selected from all files. For example,

	line 5,10 a b c
would print lines 5 and 10 of file a, followed by lines 5 and 10 of file b, followed by lines 5 and 10 of file c. To select lines from the concatenation of many input files, use cat(1) and a pipeline:
	cat a b c | line 5,10

The m-n line-selection syntax also supports ``relative'' (counted) range increments, analogous to the -p option. The notation m:n is equivalent to m-m+n-1. For example, the invocation

	line 5:10,20 inputfile
would print 10 lines beginning with line 5 (that is, lines 5 through 14), followed by line 20.


line was inspired by a program of the same name and basic functionality conceived by Philip Lantz at Tektronix MDP circa 1985.

This documentation corresponds to version 2.3 of the program.

See for possible updates.


head(1), tail(1), sed(1), column


Steve Summit,