Skip to content

Instantly share code, notes, and snippets.

Created December 28, 2016 02:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/c98dc8f6be6d414ae3e7aa6931f1c5f5 to your computer and use it in GitHub Desktop.
Save anonymous/c98dc8f6be6d414ae3e7aa6931f1c5f5 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"bytes"
"errors"
"flag"
"fmt"
"log"
"os"
)
func main() {
count, err := run()
if err != nil {
log.Fatal(err)
}
fmt.Println(count)
}
func run() (uint64, error) {
flag.Parse()
if flag.NArg() < 1 {
return 0, errors.New("missing file to search")
} else if flag.NArg() < 2 {
return 0, errors.New("missing search query")
}
fpath, query := flag.Arg(0), []byte(flag.Arg(1))
file, err := os.Open(fpath)
if err != nil {
return 0, err
}
scanner := bufio.NewScanner(file)
count := uint64(0)
for scanner.Scan() {
if bytes.Contains(scanner.Bytes(), query) {
count++
}
}
if err := scanner.Err(); err != nil {
return 0, err
}
return count, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment