summaryrefslogtreecommitdiff
path: root/ipl/progs/setmerge.icn
blob: b12598d2549224d8db606c1aceaba536d1784c5b (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
############################################################################
#
#	File:     setmerge.icn
#
#	Subject:  Program to combine sets of text items
#
#	Author:   Gregg M. Townsend
#
#	Date:     May 31, 1994
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#  Setmerge combines sets of items according to the specified operators.
#  Sets are read from files, one entry per line.  Operation is from left
#  to right without any precedence rules.  After all operations are
#  complete the resulting set is sorted and written to standard output.
# 
#  Usage:  setmerge file [[op] file]...
#
#  Operations:
#	+  add contents to set
#	-  subtract contents from set
#	*  intersect contents with set
#
#  Note that operators must be separate command options, and that some
#  shells my require some of them to be quoted.
#
#  Example 1: combine files, sorting and eliminating duplicates:
#
#	setmerge file1 + file2 + file3 + file4
#
#  Example 2: print lines common to three files
#
#	setmerge file1 '*' file2 '*' file3
#
#  Example 3: print lines in file1 or file2 but not in file3
#
#	setmerge file1 + file2 - file3
#
############################################################################


procedure main(args)
   local items, a, op, f, s

   items := set()
   op := "+"
   every a := !args do {
      if *a = 1 & any('+-*', a) then {
         op := a
         }
      else {
         f := open(a) | stop("can't open ", a)
         case op of {
            "+": every insert(items, !f)
            "-": every delete(items, !f)
            "*": {
               s := set()
               every insert(s, member(items, !f))
               items := s
               }
         }
      }
   }
   every write(!sort(items))
end