dongza3124 2017-07-06 00:38
浏览 34
已采纳

用文件中的模式替换字符串

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

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

In this case, we do NOT want to replace: 1. fmt.Printf("print speed= %d",speed) //So the key here is the ending pattern should be "). 2. log.Printf statements //only replace "fmt." Any pointers on this?

  • 写回答

2条回答 默认 最新

  • dpsfay2510 2017-07-06 01:45
    关注

    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
    ", 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
    ", speed)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启