0

I have searched and have found well-informed answers that I think should work, but I can't get them to work. The problem is that I clearly so don't understand sed and all of the slashes and what any of that has to do with regex. I think this is why I can't cross the bridge from very informative answers (such as sed: -e expression unknown option to `s') to making this work for me.

/etc/network/interface:

# WAN (Internet connection)
auto wlp3s0 
iface wlp3s0 inet dhcp
dns-nameservers 1.1.1.1 8.8.8.8 
wpa-conf /etc/wpa_supplicant_some1.conf
#wpa-conf /etc/wpa_supplicant_oldstuff.conf

bash script:

#!/bin/bash
NEW_LINE="wp-conf /etc/wpa_supplicant_someother.conf"
sed -i '/wpa-conf /c\'"$NEW_LINE" /etc/network/interfaces

The above kind of works. The entire line in the file that begins with wpa-conf is replaced with the new line in the script which is great.

But there is a nasty caveat: the commented wpa-conf line is also changed. This is what I'm trying to avoid. Reading the other answers, I've tried these in the script and a bunch of other hopeless variations that I could think of to no avail. These produce various errors:

sed -i '/^#/!s/wpa-conf /c\'"$NEW_LINE" /etc/network/interfaces
sed -i '|^#|!s|wpa-conf /c\'"$NEW_LINE" /etc/network/interfaces
sed -i '|^#|!s|wpa-conf |c\'"$NEW_LINE" /etc/network/interfaces

I clearly have no idea what I'm doing and I am evidently unable to understand how sed works and the other answers only made it worse for me.

All I'm trying to do is replace a line with another line as long as it isn't a line that is commented. The replacement line must be given in the form of a variable because it comes from other sources not shown here for brevity.

Regards and Thanks

1 Answer 1

3

I would check out the first answer on this entry. If you only want make your replacement on lines that begin with 'wpa-conf', you should add ^ and that should do what you want.

sed -i '/^wpa-conf /c\'"$NEW_LINE" /etc/network/interfaces

1
  • Bingo. I got lost in all those slashes from the examples and I couldn't even think properly. Thanks.
    – ts90
    Apr 8, 2018 at 15:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .