加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

使用Bash在特定行之前插入文本

发布时间:2020-12-15 22:24:19 所属栏目:安全 来源:网络整理
导读:如何在找到字符串的第一个位置将一组行(大约5个)插入到文件中? 例如: BestAnimals.txt dogcatdolphincat $“在cat之前将长颈鹿插入BestAnimals.txt” NewBestAnimals.txt NewBestAnimals.txt doggiraffe catdolphincat 解决方法 如果使用gnu sed: $cat an
如何在找到字符串的第一个位置将一组行(大约5个)插入到文件中?

例如:

BestAnimals.txt

dog
cat
dolphin
cat

$“在cat之前将长颈鹿插入BestAnimals.txt”> NewBestAnimals.txt

NewBestAnimals.txt

dog
giraffe 
cat
dolphin
cat

解决方法

如果使用gnu sed:

$cat animals
dog
cat
dolphin
cat

$sed  "/cat/ { N; s/catn/giraffen&/ }" animals
dog
giraffe
cat
dolphin
cat

>匹配一行(/ cat /)>继续下一行(N)>将匹配的模式替换为插入和匹配的字符串,其中&代表匹配的字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读