Previous: Dup3, Up: Finding Duplicate Lines [Index]
Modify dup2
to print the names of all files in which each duplicated line
occurs.
// Dup2 prints the count and text of lines that appear more than once // in the input. It reads from stdin or from a list of named files. // In Exercise 1.4. modify dup2 to print the names of all files in // which each duplicated line occurs. package main import ( "bufio" "fmt" "os" ) func main() { counts := make(map[string]int) files := os.Args[1:] if len(files) == 0 { fileName := os.Stdin countLines(os.Stdin, counts) } else { for _, arg := range files { fileName := arg f, err := os.Open(arg) if err != nil { fmt.Fprintf(os.Stderr, "dup2: %v\n", err) continue } countLines(f, counts) f.Close() } } for line, n := range counts { if n > 1 { fmt.Printf("%v: %d\t%s\n", fileName, n, line) } } } func countLines(f *os.File, counts map[string]int) { input := bufio.NewScanner(f) for input.Scan() { counts[input.Text()]++ } // NOTE: ignoring potential errors from input.Err() }