tgpl/ch1/dup/dup2.go

38 lines
547 B
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
counts := make(map[string]int)
file := os.Args[1:]
if len(file) == 0 {
countLines(os.Stdin, counts)
} else {
for _, i := range file {
f, err := os.Open(i)
if err != nil {
log.Print(err)
}
countLines(f, counts)
f.Close()
}
}
for lines, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, lines)
}
}
}
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
}