sed替换.bashrc中的PATH

时间:2012-07-20 22:02:13

标签: sed

我的.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

2 个答案:

答案 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