我有一个配置文件(gpsd.default)包含具有以下格式的数据:
# If you must specify a non-NMEA driver, uncomment and modify the next line
GPSD_SOCKET="/var/run/gpsd.sock"
GPSD_OPTIONS=""
GPS_DEVICES=""
我用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
上面的sed命令返回错误:
sed: bad option in substitution expression
因为新行在其表达式中包含“/”.
如何更新我的sed命令使其工作?
这是因为您使用的是包含/的正则表达式,它与sed用作分隔符的字符相同.
只需将sed分隔符更改为另一个分隔符,例如〜:
sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default
顺便说一下,由于您要更改/ etc中的文件,您可能需要使用-i.bak,以便备份原始文件.防止丢失重要信息是一种很好的做法.