【发布时间】:2014-07-20 08:38:46
【问题描述】:
我有一个 bash 脚本需要注释掉文件中第一个非注释行并显示它,如下所示:
env=$(grep -v -m 1 "#" file)
if [ "$env" != "" ]
then
sed -i "/\<$env\>/s/^/#/" file
fi
echo $env
变成这样:
#this is an example file
this is the first line
this is the second line
进入这个:
#this is an example file
#this is the first line
this is the second line
并回应这个this is the first line
我相信有一种更直接、更规范的方法可以做到这一点,也许只使用 sed。它是单行的,因为它实际上是通过 ssh 执行的。另一方面,通过 ssh 执行会导致在 sed 参数中扩展环境变量出现问题,并且使用 \\\$env 转义它不起作用。非常感谢任何建议。
【问题讨论】:
标签: bash replace sed grep comments