是否有可能运行sed,如果不存在附加在结构中?

时间:2015-06-07 06:29:40

标签: fabric

是否可以在存在特定模式的行时运行,然后修改,如果不存在,则附加文本?

类似

modified = sed('file.txt', before = 'to be replaced', after = 'expected')
if(!modified):
    append('file.txt', 'expected')

1 个答案:

答案 0 :(得分:1)

确实可以!

检查一下,了解如何使用Python sed How to do sed like text replace with python?

代码将是这样的:

file = 'file.txt'
before = 'to be replaced'
after = 'expected'

if before in file:
   sed(before, expected)
else:
   append(file, after)