我在 CentOS 上有一个很长的、维护得不好的 bash 脚本,其中有许多使用 echo 的日志行,其中大约三分之一的 tee-ing 到一个日志文件中。我想修改其余的回显行,以便也进入此日志文件。
这是一个示例 myscript.sh:
command1
echo "hi1"
echo "hi2" | tee -a my.log
echo "hi3 tee"
command2
在此文件上运行某些内容后,我希望将内容更改为:
command1
echo "hi1" | tee -a my.log
echo "hi2" | tee -a my.log
echo "hi3 tee" | tee -a my.log
command2
我在想我需要将 sed 或 awk 与正则表达式一起使用,其中逻辑是,“如果该行包含 ' echo
',后跟不是' ',则在行尾| tee
附加 ' '”。| tee -a my.log
经过大量搜索,这是迄今为止我想出的最好的:
sed --in-place=_BACKUP '/^.*echo\(?!\| tee$\)*/ s/$/ \| tee -a my.log/' myscript.sh
但这只是附加| tee -a my.log
到包含echo
.
有没有人有什么好主意?