Created
March 21, 2021 18:03
-
-
Save s1moe2/e35cd0e59c17398eebf47faf361b3540 to your computer and use it in GitHub Desktop.
Replace lines that start with a prefix in a file
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
func replaceImageInTemplate(filepath string, prefix string, newString string) error { | |
input, err := ioutil.ReadFile(filepath) | |
if err != nil { | |
return err | |
} | |
r, err := regexp.Compile(fmt.Sprintf("(?m)^%s(.*?)$", prefix)) | |
if err != nil { | |
return err | |
} | |
newFile := r.ReplaceAll(input, []byte(newString)) | |
if err = ioutil.WriteFile(filepath, newFile, 0666); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment