Created
July 4, 2012 05:45
-
-
Save ahamid/3045601 to your computer and use it in GitHub Desktop.
whole file regexp replacement in go
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 ( | |
"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