Sed 匹配字段后在其前后添加文本方法

使用Sed 匹配字段后在其前后添加文本方法:

修改文本test.txt中BUGID在其前一行加上just a test for add something ahead BUGID

sed “/匹配字段/i加上想要插入到匹配字段前一行的内容” 文件名

dwm@dwm:~/dongwuming/sed_test$ cat test.txt
just a test
BUGID: 11111111
JiraID:22222222
例如(三种格式都可以):
dwm@dwm:~/dongwuming/sed_test$
sed "/BUGID/i just a test for add something ahead BUGID" test.txt
just a test
just a test for add something ahead BUGID
BUGID: 11111111
JiraID:22222222
sed "/BUGID/i\just a test for add something ahead BUGID" test.txt
just a test
just a test for add something ahead BUGID
BUGID: 11111111
JiraID:22222222
dwm@dwm:~/dongwuming/sed_test$
sed "/BUGID/ijust a test for add something ahead BUGID" test.txt
just a test
just a test for add something ahead BUGID
BUGID: 11111111
JiraID:22222222

修改文本test.txt中BUGID在其后一行加上just a test for add something after BUGID:

sed “/匹配字段/a加上想要插入到匹配字段后一行的内容” 文件名

dwm@dwm:~/dongwuming/sed_test$ cat test.txt
just a test
BUGID: 11111111
JiraID:22222222
    例如(三种格式都可以):
sed "/BUGID/a just a test for add something after BUGID" test.txt
just a test
just a test for add something after BUGID
BUGID: 11111111
JiraID:22222222
dwm@dwm:~/dongwuming/sed_test$
sed "/BUGID/a\just a test for add something after BUGID" test.txt
just a test
just a test for add something after BUGID
BUGID: 11111111
JiraID:22222222
dwm@dwm:~/dongwuming/sed_test$
sed "/BUGID/ajust a test for add something after BUGID" test.txt
just a test
BUGID: 11111111
just a test for add something after BUGID
JiraID:22222222

你可能感兴趣的:(shell,linux)