Created
December 28, 2016 02:16
-
-
Save anonymous/c98dc8f6be6d414ae3e7aa6931f1c5f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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