I am trying to change the whole line in a file matched by part of the line only. e.g. the command below works fine as "$Netconf_DHCPStart" doesn't have a / in it:

sed -e '/DHCP_START=/c\DHCP_START='"$Netconf_DHCPStart"

However, the command below doesn't work

sed '/static ip_address='"${Current_StaticIP}"'/c\static ip_address='"$Netconf_range"'.110' /etc/dhcpcd.conf

giving error:

sed: -e expression #1, char 32: unknown command: '2'

I know that this is because "$Current_StaticIP" contains a / within it, however, I am not sure how to format it so it will work. Do I need to escape the \ within the variable first?

  • 2
    Hi @Josh. Kindly would you please include example of the input and output. This will make the question very helpful to everyone. – user88036 Sep 19 '18 at 14:20
  • 2
    Contents of variables would be nice, too. – RudiC Sep 19 '18 at 14:21
  • 1
    Linkin in: unix.stackexchange.com/questions/32907/… – Jeff Schaller Sep 19 '18 at 14:25
  • I believe Josh has clearly identified the problem: the $Current_StaticIP variable contains a forward-slash, prematurely ending the /static ip_address.../ search – Jeff Schaller Sep 19 '18 at 14:27
  • Sorry should have included them. $Netconf_DHCPStart is $Current_StaticIP is and $Netconf_range is 192.168.0. – Josh Sep 19 '18 at 15:10

You could use the alternative \c...c form, where c is any character that isn't expected to come up inside your variable:

sed '\%static ip_address='"${Current_StaticIP}"'%c\static ip_address='"$Netconf_range"'.110'

(The c\ part of your sed command isn't the problem unless your second variable contains a newline.)

  • That worked perfectly. Although the second variable doesn't contain a newline that I'm aware of - its simply – Josh Sep 19 '18 at 15:34
  • By "second variable" I mean $Netconf_range, the one after c\... But as previously stated, that's not the problem here. – JigglyNaga Sep 19 '18 at 15:35
  • Sorry looking at way too many variables at the moment. Netconf_range is 192.168.0 Anyway the % worked perfectly. Thanks for your help. – Josh Sep 19 '18 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.