0

我的条目中有以下条目.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

4

2 回答 2

0

你这样做:

NEWPATH="your_new_path" sed -i "/ADDED BY INSTALLER - DO NOT EDIT OR DELETE THIS COMMENT/ s:/home/user/abc/bin:$NEWPATH:" .bashrc
于 2012-07-20T23:56:25.060 回答
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后缀添加到就地编辑中。

于 2012-07-20T22:10:41.847 回答