Skip to content

Instantly share code, notes, and snippets.

@mamclaughlin
Last active January 9, 2020 21:41
Show Gist options
  • Save mamclaughlin/1b563bc8b33b11ea52156cb276312cc9 to your computer and use it in GitHub Desktop.
Save mamclaughlin/1b563bc8b33b11ea52156cb276312cc9 to your computer and use it in GitHub Desktop.
Exercise 1.4 from "The Go Programming Language." Prints duplicate lines along with the file name and number of occurrences
/* To Run: go run duplicates.go <file1 file2 file3 ...>
Example Output:
test was found in the following files:
tester1.txt (3 times)
-----
asdf was found in the following files:
tester2.txt (5 times)
tester3.txt (2 times)
-----
*/
package main
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, "os.Stdin")
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts, arg)
f.Close()
}
}
for line, numCount := range counts {
total := 0
for _, counter := range numCount {
if counter > 1 {
total++
}
}
if total > 0 {
fmt.Printf("-----\n%s was found in the following files:\n", line)
}
for file, counter := range numCount {
if counter > 1 {
fmt.Printf("%s\t(%d times)\n", file, counter)
}
}
}
}
func countLines(f *os.File, counts map[string]map[string]int, filename string) {
input := bufio.NewScanner(f)
for input.Scan() {
line := input.Text()
if line == "" {
continue
}
if counts[line] == nil {
counts[line] = make(map[string]int)
}
counts[line][filename]++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment