Problem using os.system() with sed command

后端 未结 7 2068
耶瑟儿~
耶瑟儿~ 2020-12-20 04:30

I\'m writing a small method to replace some text in a file. The only argument I need is the new text, as it is always the same file and text to be replaced.

I\'m hav

7条回答
  •  囚心锁ツ
    2020-12-20 04:53

    What is wrong is that there is some difference. Yeah, I know that's not helpful, but you need to figure out the difference.

    Try running this:

    import os
    def updateExportConfigId(id):
        stringId = "%s" % id
        cmd1 = "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"
        stringId = "GRRRRRRRRR"
        cmd2 = "sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=" + stringId + "/g' path/file.old > path/file.new"
    
        print "cmd1:" , cmd1
        print "cmd2:" , cmd2
        print cmd1 == cmd2
    
    updateExportConfigId("GRRRRRRRRR")
    

    The code should print:

    sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRRRRRRR/g' path/file.old > path/file.new
    sed '1,$s/MANAGER_ID=[0-9]*/MANAGER_ID=GRRRRRRRRR/g' path/file.old > path/file.new
    True
    

    Thereby showing that they are exactly the same. If the last line is "False" then they are not the same, and you should be able to see the difference.

提交回复
热议问题