【发布时间】:2021-01-17 00:19:50
【问题描述】:
我尝试使用 sed 命令替换 yml 文件中的路径,并尝试注释该文件中的旧路径。评论工作正常,但不是专门针对该路径。该文件中还有其他路径。我的 yml 文件如下所示:
enabled: true
close_eof: true
#close_inactive: 1h
paths:
- /home/directory/another_directory/directory_20200910/*_*_*
现在我希望我的文件看起来像
enabled: true
close_eof: true
#close_inactive: 1h
paths:
- /home/directory/another_directory/directory_(today's date here-20201001)/*_*_*
请注意:yml 文件有四个空格,路径前有一个“-”
SPTH='/home/directory/another_directory'
sed -e '/$SPTH/ s/^#*/#/' -i test.yml
echo - /home/directory/another_directory/directory_date +%Y%m%d >> /home/test.yml
但这似乎不起作用,另一件事是尝试过的是
sed -e '/home/ s/^#*/#/' -i test.yml
echo - /home/directory/another_directory/directory_`date +%Y%m%d` >> /home/test.yml
这很好用,但它会在每个以/home 开头的路径上添加注释,并且还会在文件末尾回显路径。因此我必须使用 sed 的东西,因为我想用当前日期替换路径。
如果我想用昨天的日期替换文件夹的日期,请告诉我,因为我想在文件夹创建一天后运行脚本。
【问题讨论】:
-
YAML 工具需要使用像
yq这样的语法感知解析器进行解析。你能安装这样的工具吗? - mikefarah.gitbook.io/yq -
我没有尝试安装,我试图使用 sed 直接替换它而不解析
标签: linux bash sed scripting yaml