Hello, i have an linux bash shell code :
sed -i -e "s/DRUMCLASS/False/g" "/usr/share/OCTAVA/Rich/HTTP/CLASS" &> /dev/null
How can we implement it in golang? i have searched in google but didn’t found any relavent result
Hello, i have an linux bash shell code :
sed -i -e "s/DRUMCLASS/False/g" "/usr/share/OCTAVA/Rich/HTTP/CLASS" &> /dev/null
How can we implement it in golang? i have searched in google but didn’t found any relavent result
maybe use strings.ReplaceAll(...)
?
How do we replace strings in a file using strings.ReplaceAll() ? I think it’s not possible via Replace all
I want the same way as sed usage and it must be fast.
hmm… I don’t know one-line solution
maybe sth like that
filePath := "/path/to/somewhere"
fileData, _ := ioutil.ReadFile(filePath)
fileString := string(fileData)
fileString = strings.ReplaceAll(fileString, "old", "new")
fileData = []byte(fileString)
_ = ioutil.WriteFile(filePath, fileData, 0o600)
It should work but idk if it is fast/save/ok
use exec.Command(“bash”, “-c”, `sed -i -e “s/DRUMCLASS/False/g” “/usr/share/OCTAVA/Rich/HTTP/CLASS” &> /dev/null)` in os/exec lib.
getting error
./5test.go:5:18: invalid identifier character U+201C ‘“’
./5test.go:5:25: invalid identifier character U+201D ‘”’
./5test.go:5:30: invalid identifier character U+201C ‘“’
./5test.go:5:35: invalid identifier character U+201D ‘”’
./5test.go:5:131: syntax error: unexpected newline, expecting comma or )
`
exec.Cmd
everywhere, you can just do it in bash. Every exec.Cmd
you do is easily traceable and recoverable.didn’t understood sir what do you mean by enclosing codes in ` ?
Exactly what I did when I wrote exec.Cmd
as `exec.Cmd`
.
It makes it easier to read, and also it stops the software from mangling your quotes.
For code blocks that form multiline code you can use triple backticks:
```
// Your multiline code
func main() {}
```
due to my english i’m unable to undertsand actually you’re saying in another way
exec.Command(“bash”, “-c”, `sed -i -e “s/DRUMCLASS/False/g” “/usr/share/OCTAVA/Rich/HTTP/CLASS” &> /dev/null)`
can you correct this ?
actually i’m not understanding what you’re saying, first you said enclose backticks in the codes i’m not sure what do u mean by enclose backticks i thought you mean to say that add ` backtick before )
so i asked you.
bro if you know what he has written then can you correct that and can tell me via multiline code ?
like
exec.Command(“bash”, “-c”, `sed -i -e “s/DRUMCLASS/False/g” “/usr/share/OCTAVA/Rich/HTTP/CLASS” &> /dev/null)`
When I said “enclose code in backticks” I meant to do so in the forum when posting, as you seem to have in your last post.
And to correct the failing code, just replace semantic quotes “”
with the syntactic ones used for strings ""
.
It’s nothing special, and you will be confronted with slight syntactic problems like this in forums, you will need to learn those lighter ones on your own.
Though again, don’t use exec.Cmd
if you don’t have to. You can do everything sed
can do in go.
If you keep chaining just exec.Cmd
s you can just stick to a single bash script, it will be much smaller in size, easier to debug, and even faster!
btw, @NobbZ how sed
stuff should be done in golang? Is my code above correct?
As I said, in my opinion one shouldn’t invoke sed
from go, but instead do the replace job within go.
i have done this and it’s working properly :
func sed(old string, new string, file string) {
filePath := file
fileData, _ := ioutil.ReadFile(filePath)
fileString := string(fileData)
fileString = strings.ReplaceAll(fileString, old, new)
fileData = []byte(fileString)
_ = ioutil.WriteFile(filePath, fileData, 0o600)
}
filePath := file
func sed(old, new, filePath string) error {
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
fileString := string(fileData)
fileString = strings.ReplaceAll(fileString, old, new)
fileData = []byte(fileString)
err = ioutil.WriteFile(filePath, fileData, 0o600)
if err != nil {
return err
}
return nil
}
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.