Skip to content
Advertisement

Get rid of single quotes from the substituted string within sed command in Python

I have a text file 1.txt which has following content:

JavaScript

I am trying to generate multi-line string that I need to add before module abc line in 1.txt. I am using sed to do this.

JavaScript

This is the sed command I am forming and printing before executing it:

JavaScript

When I execute the above sed command, I get error:

JavaScript

There shouldn’t be ' before and after the string that gets substituted in the sed command, but not sure how to get rid of them. This behavior is with Python3.10.

This works fine:

sed -i ‘/module abc/i `include “a_defs.vh”n`include “b_defs.vh”n`include “c_defs.vh”‘ 1.txt

This is the behavior in Python2.7:

JavaScript

I tried repr, strip, but didn’t help. Can someone please help me resolving this?

Advertisement

Answer

If you want to convert newlines to n characters (which means ‘newline’ to sed) just substitute them using following statement:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement