Messing around with I/O streams and regexes

This commit is contained in:
Dan Buch 2012-11-10 20:27:23 -05:00
parent 1ab8d7d42c
commit d0fbe716d9
2 changed files with 55 additions and 0 deletions

View File

@ -4,6 +4,10 @@ PACKAGES := $(foreach pkg,\
$(shell ls src/meatballhat.com/gotour-artifacts),\ $(shell ls src/meatballhat.com/gotour-artifacts),\
$(patsubst %,meatballhat.com/gotour-artifacts/%,$(pkg))\ $(patsubst %,meatballhat.com/gotour-artifacts/%,$(pkg))\
) )
PACKAGES += $(foreach pkg,\
$(shell ls src/meatballhat.com/junkdrawer),\
$(patsubst %,meatballhat.com/junkdrawer/%,$(pkg))\
)
PACKAGES += meatballhat.com/amqpfun-runner PACKAGES += meatballhat.com/amqpfun-runner
test: build test: build

View File

@ -0,0 +1,51 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"regexp"
)
const USAGE = `Usage: greppish <regex> [filename]
Prints lines matching <regex> in [filename] or standard input.`
func main() {
if len(os.Args) < 2 {
fmt.Println(USAGE)
os.Exit(1)
}
regMatch, err := regexp.Compile(os.Args[1])
if err != nil {
log.Fatal("Problem compiling regex: ", err)
}
inbuf := bufio.NewReader(os.Stdin)
if len(os.Args) > 2 {
infile, err := os.Open(os.Args[2])
if err != nil {
log.Fatal("Problem opening input file: ", err)
}
inbuf = bufio.NewReader(infile)
}
var line string
for {
line, err = inbuf.ReadString('\n')
if err != nil {
if err == io.EOF {
os.Exit(0)
}
log.Fatal("Problem reading input: ", err)
}
if regMatch.MatchString(line) {
fmt.Printf(line)
}
}
}