Bash - 如何在 bash 脚本中的特定行号后在 rst 文件中输入行

si0two

我想我的简单脚本有问题。

情况:我的 repo 中有 rst 文件,我只需要找到 .rst 文件(来自 repo),将它们的名称插入到特定行 (11) 的其他 rst 文件中。我需要输入的这一行必须被剪切 - 没有 .rst 并且在我的情况下也没有 ./ 在文件名之前。

例如:main.rst包含几行
线1
...
3号线


在这里应该是插入first_file
这里应该是插入second_file

行14

./first_file.rst必须在没有 .rst 和 ./ 部分的特定行中插入到main.rst中。我还需要在 repo 中只找到 .rst 文件。查找部分很容易,我想只需找到即可。-名称 '*.rst'在那之后,我认为我应该使用 sed 但我不知道如何正确使用它,我想在一行中使用此 find 行来完成。我将不胜感激任何建议。

卡米尔库克

如果要插入的输出find . -name '*.rst'.rst去掉后缀为一些文件的第11行,您可以:

# generate the list with .rst files
# I use `printf "%f\n"` to print only the filename, ie. remove the  `./` part
# sed is used to remove the .rst
find . -name '*.rst' -printf "%f\n" | sed 's/.rst$//' > sometempfile.txt
# Insert the content of sometempfile.txt into 11nth line in the main.rst file
sed -i -e '11r sometempfile.txt' main.rst
# pick up the trash
rm sometempfile.txt

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章