Issue
I am using sed -e in a python script to comment and uncomment lines in my /etc/network/interfaces file for my RPi2. It works in such a way that when I run my python script, the specific lines in my /etc/network/interfaces will be commented and uncommented respectively. However, I am facing an error where I cannot comment out some of the lines, in other words, it does not add a '#' in front of the line I want commented out. I am also facing problems uncommenting the wpa-conf /etc/wpa_supplicant.conf
This is the code that I used for commenting in my python script:
stream = os.popen("sudo sed -e '/iface wlan0 inet static/ s/^#*/#/' -i /etc/network/interfaces
I have about 4 other almost identical lines, just the part where 'iface....static' changes.
The code that I used for uncommenting the wpa-conf /etc/wpa_supplicant.conf is this:
stream = os.popen("sudo sed -e '/wpa-conf /etc/wpa_supplicant.conf/ s/^#*//' -i /etc/network/interfaces
Error that came out when I ran the python was:
sh :1 tc/wpa_supplicant.conf/: not found
The python script managed to comment out all but 1 line, and uncomment out all but just the wpa-conf /etc/wpa_supplicant.conf line.
Would appreciate any assistance.
Solution
You need to escape the forward slashes in this match /wpa-conf /etc/wpa_supplicant.conf/
to /wpa-conf \/etc\/wpa_supplicant.conf/
What is happening is that sed sees /wpa-conf /e
which tells it to execute the subsequent command on lines matching /wpa-conf /
. The "subsequent command" is tc/wpa_supplicant.conf/
, which is not a valid sed command.
Answered By - stevesliva