summaryrefslogtreecommitdiff
path: root/ipl/progs/getlines.icn
blob: 5c1b3439d7263e71421956a63d4b0d12a093225b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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