diff options
Diffstat (limited to 'ipl/progs/getlines.icn')
-rw-r--r-- | ipl/progs/getlines.icn | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/ipl/progs/getlines.icn b/ipl/progs/getlines.icn new file mode 100644 index 0000000..5c1b343 --- /dev/null +++ b/ipl/progs/getlines.icn @@ -0,0 +1,54 @@ +############################################################################ +# +# File: getlines.icn +# +# Subject: Program to extract lines from a file +# +# Author: Ralph E. Griswold +# +# Date: March 26, 2002 +# +############################################################################ +# +# This file is in the public domain. +# +############################################################################ +# +# This program is designed to extract a few specified lines from a file. +# The line numbers are given on the command line, the file is read from +# standard input and the extracted lines are written to standard output +# as in +# +# getlines 46 23 119 <infile >outfile +# +# which writes lines 23, 46, and 119 of infile (if it contains that many +# lines) to outfile. +# +# Line numbers do not have to be given in order. Numbers less than 1 are +# ignored, but a nonnumerical argument is treated as an error. +# +############################################################################ + +procedure main(lines) + local i, line + + if *lines = 0 then stop("*** no lines specified") + + every i := 1 to *lines do + lines[i] := integer(lines[i]) | + stop("*** nonnumeric argument: ", image(lines[i])) + + lines := set(lines) # inefficient method but easy + + i := 0 + + while line := read() do { + i +:= 1 + if member(lines, i) then { + write(line) + delete(lines, i) # so process can be stopped before end + if *lines = 0 then exit() + } + } + +end |