现在的位置: 首页 > 综合 > 正文

sed 在指定行后添加新数据

2013年10月17日 ⁄ 综合 ⁄ 共 509字 ⁄ 字号 评论关闭
文章目录

1. sed 的 追加 a 选项

cat txt
aaa 111 bbb
ccc 222 ddd
eee 333 ggg
jjj 444 kkk
hhh 555 lll
mmm 666 nnn
jjj 444 kkk
kk 777 ggg

(1). a 追加一行数据

sed -i -e '/jjj 444 kkk/a\test test test' txt
cat txt
aaa 111 bbb
ccc 222 ddd
eee 333 ggg
jjj 444 kkk
test test test  #追加
hhh 555 lll
mmm 666 nnn
jjj 444 kkk
test test test  #追加
kk 777 ggg

(2). a 追加多行数据

sed -i -e '/jjj 444 kkk/a\one one one\ntwo two two\nthree three three' txt
cat txt
aaa 111 bbb
ccc 222 ddd
eee 333 ggg
jjj 444 kkk
one one one
two two two
three three three
hhh 555 lll
mmm 666 nnn
jjj 444 kkk
one one one
two two two
three three three
kk 777 ggg

注意:

  • 不用加 g选项 就默认把符合条件的都加入  a\ 后面的内容
  • 加入一行默认是自动换行;最后一行不用加 \n

抱歉!评论已关闭.