-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
58 lines (47 loc) · 869 Bytes
/
main.go
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
package main
// Usage:
// go run main.go 1.txt 2.txt 3.txt
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
continue
}
countLines(f, counts)
f.Close()
}
}
for line, fileMap := range counts {
total := 0
for _, n := range fileMap {
total += n
}
if total > 1 {
fmt.Printf("%d %s\n", total, line)
for file, n := range fileMap {
fmt.Printf("%d %s\n", n, file)
}
}
}
}
func countLines(f *os.File, counts map[string]map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
line := input.Text()
if counts[line] == nil {
m := make(map[string]int)
counts[line] = m
}
counts[line][f.Name()]++
}
}