我的.bashrc
中有以下条目:
PATH=$PATH:/home/user/abc/bin;export PATH; # ADDED BY INSTALLER - DO NOT EDIT OR DELETE THIS COMMENT
我想用/home/user/abc/bin
替换我选择的任何路径。如何使用sed
?
我有以下内容:
sed 's,PATH=[^;]*,PATH=$PATH/home/user/app/2.1,' -i ~/.bashrc
但是,这将取代以PATH
开头的任何内容,如何指定仅选择以PATH
开头的内容,还包括字符串ADDED BY INSTALLER - DO NOT EDIT OR DELETE THIS COMMENT
答案 0 :(得分:0)
锚点到开头(^
)和行尾($
),如下所示:
sed -i.bak -e 's,^PATH=[^;]*\(.*ADDED BY INSTALLER - DO NOT EDIT OR DELETE THIS COMMENT\)$,PATH=$PATH/home/user/app/2.1\1,' ~/.bashrc
为了安全起见,我已将.bak
后缀添加到就地编辑中。
答案 1 :(得分:0)
你这样做:
NEWPATH="your_new_path" sed -i "/ADDED BY INSTALLER - DO NOT EDIT OR DELETE THIS COMMENT/ s:/home/user/abc/bin:$NEWPATH:" .bashrc