sed错误:替换表达式中的错误选项 [英] Sed error : bad option in substitution expression
问题描述
我有一个配置文件(gpsd.default
),其中包含以下格式的数据:
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=""
我正在使用sed更改文件:
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
上面的sed
命令返回错误:
sed:替换表达式中的错误选项
sed: bad option in substitution expression
因为新行的表达式中包含"/
".
Because the new line contains "/
" in its expression.
如何更新我的sed
命令以使其起作用?
How to update my sed
command to make it work?
推荐答案
这是因为您正在使用包含/
的正则表达式,该正则表达式与sed
用作分隔符的字符相同.
This is because you are using a regex containing /
, which is the same character sed
uses as delimiter.
只需将sed
分隔符更改为另一个,例如~
:
Just change the sed
delimiter to another one, for example ~
:
sed -i 's~^GPS_DEVICES="".*~GPS_DEVICES="dev/ttyUSB1"~' /etc/default/gpsd.default
顺便说一句,由于要在/etc
中更改文件,因此可能要使用-i.bak
,以便备份原始文件.防止重要信息丢失是一个好习惯.
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.
这篇关于sed错误:替换表达式中的错误选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!