Skip to content

Instantly share code, notes, and snippets.

@ahamid ahamid/replace.go
Created Jul 4, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.