英文:
replace string with patterns in file
问题
我已经使用sed命令在*.go文件中替换变量,命令如下:sed -i 's/<old_name>/newName/g' *.go
。
我的目标是消除golinter错误。
如何替换具有共同模式的字符串,例如将1替换为2:
fmt.Printf("blah blah blah")
或fmt.Printf("yadda yadda yadda")
fmt.Println("blah blah blah")
或fmt.Println("yadda yadda yadda")
在这种情况下,我们不想替换:
fmt.Printf("print speed= %d",speed)
//因此关键在于结束模式应为")
。- log.Printf语句 //只替换"fmt."
对此有什么指导吗?
英文:
I've used sed to replace variables on *.go files using sed -i 's/\<old_name\>/newName/g' *.go
My objective is to eliminate golinter errors.
How can strings with common patterns, e.g. replace 1 with 2
fmt.Printf("blah blah blah")
orfmt.Printf("yadda yadda yadda")
fmt.Println("blah blah blah")
orfmt.Println("yadda yadda yadda")
In this case, we do NOT want to replace:
fmt.Printf("print speed= %d",speed)
//So the key here is the ending pattern should be")
.- log.Printf statements //only replace "fmt."
Any pointers on this?
答案1
得分: 0
我对你的问题有点困惑,但我认为你想要做以下操作:
将 printf("yada yada yada")
替换为 println("yada yada yada")
不要替换 printf("print speed = %d", speed)
如果是这样的话,我会这样做:
sed -i '/Printf(".*")/ s/Printf/Println/g' *.go
这样做会保留你实际上想要使用格式化的情况。以下是一个示例:
[sborza@msandn]:~$ cat tester.go
package main
import "fmt"
func main() {
speed = 1
fmt.Printf("vim-go")
fmt.Printf("speed = %d\n", speed)
}
[sborza@msandn]:~$ sed '/Printf(".*")/ s/Printf/Println/g' tester.go
package main
import "fmt"
func main() {
speed = 1
fmt.Println("vim-go")
fmt.Printf("speed = %d\n", speed)
}
希望这能帮到你!
英文:
I'm slightly confused by your question, but think you're trying to do the following:
replace printf("yada yada yada") with println("yada yada yada")
not replace printf("print speed = %d", speed)
If that is the case, I would do something like the following:
sed -i '/Printf(\".*\")/ s/Printf/Println/g' *.go
That should leave cases intact where you actually want to use formatting. Here is an example:
[sborza@msandn]:~$ cat tester.go
package main
import "fmt"
func main() {
speed = 1
fmt.Printf("vim-go")
fmt.Printf("speed = %d\n", speed)
}
[sborza@msandn]:~$ sed '/Printf(\".*\")/ s/Printf/Println/g' tester.go
package main
import "fmt"
func main() {
speed = 1
fmt.Println("vim-go")
fmt.Printf("speed = %d\n", speed)
}
答案2
得分: 0
请注意,在执行此操作之前,请先备份文件。
执行以下命令:
gofmt -w -r "printf -> println" .
注意,在这之前要先备份文件。
英文:
Do
gofmt -w -r "printf -> println" .
Note that make backup file before do this.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论