是否可以在存在特定模式的行时运行,然后修改,如果不存在,则附加文本?
类似
modified = sed('file.txt', before = 'to be replaced', after = 'expected')
if(!modified):
append('file.txt', 'expected')
答案 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)