linux - 使用 bash 在文件中完全匹配后逐行插入数组项
问题描述
我在 Bash 中有一个字符串数组。我想在文件中搜索模式(例如:[##Test##])并在匹配后逐行插入所有这些数组项。
我怎样才能做到这一点?
示例输入文件:
This the Title of file
//empty line
Some text here
====================
[##Test##]
- test1
- test2
====================
some other text here
样本数组:
("- test3", "- test4", "", "- test5", "", "- test6")
在文件中查找并在此之后插入数组的模式:
[##Test##]
示例输出文件:
This the Title of file
//empty line
Some text here
====================
[##Test##]
- test3
- test4
- test5
- test6
- test1
- test2
====================
some other text here
我尝试使用sed
,但我只知道如何在匹配后插入一行,而不是整个数组。
解决方案
如果ed
是可以接受的,因为ed
它在 Mac 上可用。
array=("- test3" "- test4" "" "- test5" "" "- test6")
printf '%s\n' '/\[##Test##\]/a' "${array[@]}" . ,p Q | ed -s file.txt
如果需要就地编辑,请更改Q
为。w
推荐阅读
- css - 表格边框不可见
- javascript - 调试 NodeJS 中的 404 和 502 错误
- linux - 使用 ptrace 覆盖字符串失败
- java - 如何创建然后将输入从第一个面板分配到一个 JFrame 中的不同面板?公式中的错误是什么?
- postgresql - 在 SQLAlchemy ORM 中表达 Postgresql VALUES 命令?
- python - 从文件中删除一个单词
- laravel - 如何解决这个错误 SQLSTATE[HY000]: General error: 1005 Can't create table
- angular - 角度材料分页和排序问题
- javascript - 我在尝试在 Vue 中重写此 JavaScript 点击事件时遇到问题
- spring-boot - 如何使用spring boot发送邮件?