1

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')
Adrian Seungjin Lee
  • 1,646
  • 2
  • 15
  • 28

1 Answers1

1

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)
Community
  • 1
  • 1