我有一个 bash 脚本,它有大约 2000 行代码,在这个脚本的各个行中,脚本将一些状态消息写入日志文件,即 LogFiles.txt,bills.txt 我想为所有人评论(搜索并替换文本)仅在 LogFiles.txt 中写入状态消息的行
示例脚本文件:
echo "+++++++++++++++++++++">>bills.txt
echo "doing some stuff">>bills.txt
echo "starting
to execute some commands">>LogFiles.txt
echo "----------------------">>LogFiles.txt
ls
cat someFile.txt| grep "search me"
echo "search results found">>LogFiles.txt
echo "----------------------">>LogFiles.txt
echo "+++++++++++++++++++++">>bills.txt
echo "doing some more stuff">>bills.txt
some other commands...
echo "finshing
script
execution">>LogFiles.txt
echo "----------------------">>LogFiles.txt
所需的输出:
echo "+++++++++++++++++++++">>bills.txt
echo "doing some stuff">>bills.txt
/*echo "starting
to execute some commands">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
ls
cat someFile.txt| grep "search me"
/*echo "search results found">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
echo "+++++++++++++++++++++">>bills.txt
echo "doing some more stuff">>bills.txt
some other commands...
/*echo "finshing
script
execution">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
到目前为止,我已经使用了以下命令,但结果并不好:
sed -e 's/echo/\/\*echo/gI' -e 's/LogFiles.txt/LogFiles.txt\*\//gI' samplescript.sh
此命令产生的结果:
/*echo "doing some stuff">>bills.txt
/*echo "starting
to execute some commands">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
ls
cat someFile.txt| grep "search me"
/*echo "search results found">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
some other commands...
/*echo "finshing
script
execution">>LogFiles.txt*/
/*echo "----------------------">>LogFiles.txt*/
当 sed -e 命令的第一部分用 /*echo 替换所有回声时出现问题,这是一种错误的方法,因为我不需要为 bills.txt 注释回声。