Skip to content

Instantly share code, notes, and snippets.

@ahamid
Created July 4, 2012 05:45
Show Gist options
  • Save ahamid/3045601 to your computer and use it in GitHub Desktop.
Save ahamid/3045601 to your computer and use it in GitHub Desktop.
whole file regexp replacement in go
package main
import (
"regexp"
"os"
"io/ioutil"
)
func main() {
regexp, _ := regexp.Compile(os.Args[1])
replacement := []byte(os.Args[2])
var b []byte
var out *os.File
if len(os.Args) > 3 {
b, _ = ioutil.ReadFile(os.Args[3])
out, _ = os.OpenFile(os.Args[3], os.O_WRONLY, 644)
} else {
b, _ = ioutil.ReadAll(os.Stdin)
out = os.Stdout
}
out.Write(regexp.ReplaceAll(b, replacement))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment