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

bash – 如何在指定行的开头和结尾插入文本?

发布时间:2020-12-16 01:57:53 所属栏目:安全 来源:网络整理
导读:我想在指定的数字行的开头和结尾插入一些文本,就像我有这个txt文件: applebananaorangepineapple 要插入第一行的开头和结尾我使用: while read -r linedosed "1i[text_insert]$line" outputfile1done inputfilewhile read -r linedosed "1i$line[text_inse
我想在指定的数字行的开头和结尾插入一些文本,就像我有这个txt文件:

apple
banana
orange
pineapple

要插入第一行的开头和结尾我使用:

while read -r line
do
sed "1i[text_insert]$line" > outputfile1
done < inputfile

while read -r line
do
sed "1i$line[text_insert2]" > outputfile2
done < outputfile1

我获得:

[text_insert]apple[text_insert2]
banana
orange
pineapple

现在我想在第2行添加一些文字:

[text_insert]apple[text_insert2]
[text_insert3]banana[text_insert4]
orange
pineapple

我尝试使用相同的东西,但这不起作用,我发现的所有其他可能性是在指定的行之前插入像新行的文本,而不是在指定的行中添加它.

解决方法

请尝试以下代码段:

sed '2s/.*/[text_insert] & [text_insert2]/' file.txt

&安培;有一个特殊的含义:它是替换s左侧部分的匹配部分///

例如:

$cat file.txt 
a
b
c

$sed '2s/.*/[text_insert] & [text_insert2]/' file.txt 
a
[text_insert] b [text_insert2]
c

(编辑:李大同)

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

    推荐文章
      热点阅读