is it possible to run if a line of certain pattern exists, then modify, if not, append text?
something like
modified = sed('file.txt', before = 'to be replaced', after = 'expected')
if(!modified):
append('file.txt', 'expected')
is it possible to run if a line of certain pattern exists, then modify, if not, append text?
something like
modified = sed('file.txt', before = 'to be replaced', after = 'expected')
if(!modified):
append('file.txt', 'expected')
Indeed you can!
Check this out to give you a idea how to sed using Python: How to do sed like text replace with python?
The code will be something like this:
file = 'file.txt'
before = 'to be replaced'
after = 'expected'
if before in file:
sed(before, expected)
else:
append(file, after)