15

I have a configuration file (gpsd.default) containing data with the following format:

# If you must specify a non-NMEA driver, uncomment and modify the next line     
GPSD_SOCKET="/var/run/gpsd.sock"                                                
GPSD_OPTIONS=""                                                                
GPS_DEVICES="" 

I am making a change on the file with sed:

sed -i 's/^GPS_DEVICES="".*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default
or 
sed -i '4s/^.*/GPS_DEVICES="dev/ttyUSB1"/' /etc/default/gpsd.default

The above sed command returns error:

sed: bad option in substitution expression

Because the new line contains "/" in its expression.

How to update my sed command to make it work?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
ogs
  • 1,139
  • 8
  • 19
  • 42
  • The `/` isn't your only potential problem, see http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed/29626460#29626460 – Ed Morton May 05 '15 at 15:10

2 Answers2

40

This is because you are using a regex containing /, which is the same character sed uses as delimiter.

Just change the sed delimiter to another one, for example ~:

sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default

By the way, since you are changing files in /etc, you may want to use -i.bak, so that the original file gets backed up. It is a good practice to prevent loss of important information.

user3439894
  • 7,266
  • 3
  • 17
  • 28
fedorqui
  • 275,237
  • 103
  • 548
  • 598
4

You should update your sed command to this.

sed -i 's/^GPS_DEVICES=\"\".*/GPS_DEVICES=\"dev\/ttyUSB1\"/' /etc/default/gpsd.default
Harshad Yeola
  • 1,060
  • 9
  • 10