我有一个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替换所有echo时这是一种错误的方法,因为我不需要为bills.txt注释回声。
答案 0 :(得分:1)
使用: '
和'
语法在bash中进行多行注释,如@Aserre指出,使用sed:
sed -r -e "/^echo/ {/>>/ bb; :a; N; />>/! ba; :b; />>LogFiles\.txt/I {s/^echo/: ' echo/; s/(>>.*)$/\1 '/}}" samplescript.sh
还可以使用-i
直接在脚本文件上写入。
Sed命令解释:
/^echo/ { # in a line that starts with 'echo'
/>>/ bb # if also already contains '>>' jump forward to 'b' label
:a # 'a' label to jump to
N # read next line and add it to pattern space
/>>/! ba # if pattern space not contains '>>' jump back to 'a' label
:b # 'b' label to jump to
/>>LogFiles\.txt/I { # now if pattern space contains '>>LogFiles.txt' case insensitive
s/^echo/: ' echo/ # add open comment before 'echo'
s/(>>.*)$/\1 '/ # add close comment at the end of the line with '>>'
}
}
答案 1 :(得分:0)
以下简单position: absolute
可能对您有所帮助。
awk
如果您想将输出存储到Input_file中,请在上面的代码中附加awk '/LogFiles.txt$/{$0="##"$0} 1' Input_file
。
解决方案第二: 使用> temp_file && mv temp_file Input_file
备份实际的Input_file。
sed
答案 2 :(得分:0)
请勿使用注释进行配置。重写您的脚本以允许配置某些块。
: ${LOG_FILE:=LogFile.txt}
{
echo "+++++++++++++++++++++"
echo "doing some stuff"
} >> bills.txt
{
echo "starting
to execute some commands"
echo "----------------------"
} >> "$LOG_FILE"
ls
cat someFile.txt| grep "search me"
{
echo "search results found"
echo "----------------------"
} >> "$LOG_FILE"
{
echo "+++++++++++++++++++++"
echo "doing some more stuff"
} >> bills.txt
some other commands...
{
echo "finshing
script
execution"
echo "----------------------"
} >> "$LOG_FILE"
现在,如果要禁用写入日志文件,只需使用
运行脚本即可LOG_FILE=/dev/null ./sampleScript.sh
而不是评论线。