linux - 在字符串中找到模式后评论 bash 行

标签 linux bash awk sed

我有一个 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 注释 echo。

最佳答案

在 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 '>>'
  }
}

关于linux - 在字符串中找到模式后评论 bash 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580936/

相关文章:

bash - shell 脚本中的 while 条件在条件检查中带有 -h

linux - 开关/大小写在 awk 中不起作用

awk - 使用 awk 对重复字段进行分组

awk - win-7 cmd 上的 GNU awk,不会将输出重定向到文件

linux - 我可以在 Linux 中为长控制台命令创建快捷方式吗?

linux - cron 的替代品?

linux - 使用 Tesseract 对图像进行文本识别

linux - 如何使用 sed 删除 1 或 2 个点和 1 个斜线?

linux - 如何从另一个文件中读取文件

php - 为什么下面的 php 代码没有返回预期值?