-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdedup.go
More file actions
68 lines (60 loc) · 1.29 KB
/
Copy pathdedup.go
File metadata and controls
68 lines (60 loc) · 1.29 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
66
67
68
/*
* Synopsis:
* Remove duplicate lines on stdin and write to stdout.
* Note:
* dedup seems to be about twice as fast as "sort -u", when
* LANG=en_US.UTF-8; otherwise, "sort -u" about 4 time fast.
*
* 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)
}