1. 向文件中插入文本: sed '具体行数n /a插入的内容' 文件名
sed '具体行数n /i插入的内容' 文件名
例如---- sed '3 a/this is a test program.' test
sed '3 i/this is a test program.' test
原文是: abcd
how is a test
wow is not a wow
to the moon
fush
更新后是: (用参数a /,将在n后一行插入新行)
abcd
how is a test
wow is not a wow
this is a test program.
to the moon
fush
(用参数i /,将在n前一行插入新行)
abcd
how is a test
this is a test program.
wow is not a wow
to the moon
fush
2.向文件中插入文本: sed '/adcd/a/this is a test program.' test
sed '/adcd/i/this is a test program.' test
用于字符串匹配。
更新后是: (用参数a/,将在匹配字符串后一行插入新行)
abcd
this is a test program.
how is a test
wow is not a wow
to the moon
fush
(用参数i/,将在匹配字符串前一行插入新行)
this is a test program.
abcd
how is a test
wow is not a wow
to the moon
fush
tips:可以用参数-i把更新后的结果写回原文件。