Search code examples
pythonpython-3.xlinuxsed

python + run sed command in python


I created python3 module file ( sed_util_conf.py ) that include sed Linux command that replaced old string with new for example - key - http-server.https.port and value )

def run_cli(action_details, command_to_run):
    print(action_details)
    print(command_to_run)
    errCode = os.system(command_to_run)
    if errCode != 0:
        print('failed, ' + command_to_run)
        sys.exit(-1)
    print(action_details + ' - action done')


def replace_line_do(File, OLD, NEW):
.
.
.

run_cli('run_sed_cli', f'sed -ie s/^{OLD}/{NEW}/  {File}')

on other python3 script I used the following python3 syntax in order to replace the "OLD" with new "NEW"

 sed_util_conf.replace_line_do(f'{file_path}', 'http-server.https.port=.*', f'http-server.https.port={val}')

example:

 sed_util_conf.replace_line_do(f'/etc/zomer_app/conf/config.zomer', 'http-server.https.port=.*', f'http-server.https.port={val}')

script is working and changed the OLD string with NEW string in file , but additionally we get another file that ended with "e"

for example

if we replaced the line on file - /etc/zomer_app/conf/config.zomer

then we get two files

/etc/zomer_app/conf/config.zomer
/etc/zomer_app/conf/config.zomere

the reason for that is the "e" charecter in syntx - run_cli('sed', f'sed -ie s/^{OLD}/{NEW}/ {File}')

from sed man page "e" means "e script, --expression=script , add the script to the commands to be executed "

but I am not understand how this "e" in sed create the additional file - /etc/zomer_app/conf/config.zomere ?


Solution

  • You should replace -ie with -i -e.

    You cannot stack options one after the other.