-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdedup.go
More file actions
65 lines (57 loc) · 1.17 KB
/
dedup.go
File metadata and controls
65 lines (57 loc) · 1.17 KB
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
/*
* Synopsis:
* Remove duplicate lines on stdin and write to stdout.
* Note:
* A clang version exists in setspace, which will eventually replace this
* golang version. surprisingly, this golang version is only about %25
* slower.
*/
package main
import (
"bufio"
"fmt"
"os"
)
func die(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "dedup: ERROR: " + format + "\n", args...)
fmt.Fprintf(
os.Stderr,
"usage: dedup [--count]",
)
os.Exit(1)
}
func main() {
var seen map[string]bool
var buf[4096 * 4096]byte
put_count := false
argv := os.Args[1:]
argc := len(argv)
if argc == 1 {
if argv[0] != "--count" {
die("unknown option: %s", argv[0])
}
put_count = true
} else if argc != 0 {
die("bad cli arg count: got %d, need 1 or 0", argc)
}
in := bufio.NewScanner(os.Stdin)
in.Buffer(buf[:], len(buf))
seen = make(map[string]bool, 4096 * 4096)
for in.Scan() {
txt := in.Text()
if !seen[txt] {
seen[txt] = true
if put_count == false {
fmt.Println(txt)
}
}
}
if err := in.Err(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
if put_count {
fmt.Printf("%d\n", len(seen))
}
os.Exit(0)
}