在文件中使用模式替换字符串。

huangapple go评论94阅读模式
英文:

replace string with patterns in file

问题

我已经使用sed命令在*.go文件中替换变量,命令如下:sed -i 's/<old_name>/newName/g' *.go
我的目标是消除golinter错误。
如何替换具有共同模式的字符串,例如将1替换为2:

  1. fmt.Printf("blah blah blah")fmt.Printf("yadda yadda yadda")
  2. fmt.Println("blah blah blah")fmt.Println("yadda yadda yadda")

在这种情况下,我们不想替换:

  1. fmt.Printf("print speed= %d",speed) //因此关键在于结束模式应为")
  2. log.Printf语句 //只替换"fmt."
    对此有什么指导吗?
英文:

I've used sed to replace variables on *.go files using sed -i &#39;s/\&lt;old_name\&gt;/newName/g&#39; *.go
My objective is to eliminate golinter errors.
How can strings with common patterns, e.g. replace 1 with 2

  1. fmt.Printf(&quot;blah blah blah&quot;) or fmt.Printf(&quot;yadda yadda yadda&quot;)
  2. fmt.Println(&quot;blah blah blah&quot;) or fmt.Println(&quot;yadda yadda yadda&quot;)

In this case, we do NOT want to replace:

  1. fmt.Printf(&quot;print speed= %d&quot;,speed) //So the key here is the ending pattern should be &quot;).
  2. 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(&quot;yada yada yada&quot;) with println(&quot;yada yada yada&quot;)
not replace  printf(&quot;print speed = %d&quot;, speed)

If that is the case, I would do something like the following:

sed -i &#39;/Printf(\&quot;.*\&quot;)/ s/Printf/Println/g&#39; *.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 &quot;fmt&quot;

func main() {
        speed = 1
        fmt.Printf(&quot;vim-go&quot;)
        fmt.Printf(&quot;speed = %d\n&quot;, speed)
}

[sborza@msandn]:~$ sed &#39;/Printf(\&quot;.*\&quot;)/ s/Printf/Println/g&#39; tester.go
package main

import &quot;fmt&quot;

func main() {
        speed = 1
        fmt.Println(&quot;vim-go&quot;)
        fmt.Printf(&quot;speed = %d\n&quot;, speed)
}

答案2

得分: 0

请注意,在执行此操作之前,请先备份文件。

执行以下命令:

gofmt -w -r "printf -> println" .

注意,在这之前要先备份文件。

英文:

Do

gofmt -w -r &quot;printf -&gt; println&quot; .

Note that make backup file before do this.

huangapple
  • 本文由 发表于 2017年7月6日 08:38:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/44938050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定