Using pysed in script
Question: I can run successfully on command line but having problems in python script.
It complains of the second double quote.

    
    
    pysed -r "192.168.33.10" "$NEW_IP" FILE --write
                           ^
    SyntaxError: invalid syntax
    

How can I run this inside a script?


Answer: It's true that the module has no documentation for using it as a library. But
digging around the source, you can figure out how to use it.

For example:

    
    
    import shlex
    from pysed import main as pysedmain
    
    pattern = '192.168.33.10'
    new_ip = '192.168.0.1'
    filename = '/path/to/my/file.txt'
    
    command_line_args = '-r "{pattern}" "{replacement}" {filename}'.format(pattern=pattern, replacement=new_ip, filename=filename)
    args = shlex.split(command_line_args)
    isWrite = True
    
    with open(filename, 'rU') as f:
            data = f.read()
            pysedmain.executeArguments(args, data, filename, isWrite)
    

