test.txt 内容
abc
efg
asdf
abcd
bfabc
1.在内容为abc的前一行插入123
sed -e "/^abc$/i\123" test.txt
修改后内容:
123
abc
efg
asdf
abcd
bfabc
2.将含有abc字符串的行替换为123
sed -e "/abc/c\123" test.txt
修改后内容:
123
efg
asdf
123
bfabc
123
3.在内容为abc的后一行插入123
sed -e "/^abc$/a\123" test.txt
修改后内容:
abc
123
efg
asdf
abcd
123
bfabc
123
4.在含有abc的行前插入123
sed -e "/abc/ s/^/123/"
修改后内容:
123abc
efg
asdf
123abcd
123bfabc
4.在含有abc的行尾插入123
sed -e "/abc/ s/^/123/"
修改后内容:
abc123
efg
asdf
abcd123
bfabc123
5.删除含有abc的行
sed -e "/abc/d" test.txt
修改后的内容:
efg
asdf
6.在字符串abc前插入123
sed -e 's/abc/123&'
修改后的内容:
123abc
efg
asdf
123abcd
bf123abc
6.在字符串abc前插入123
sed -e 's/abc/&123'
修改后的内容:
abc123
efg
asdf
abc123d
bfabc123
7.在行首插入123
sed -e 's/^/123'
修改后内容为:
123abc
123efg
123asdf
123abcd
123bfabc
8.在行尾插入123
修改后内容为:
abc123
efg123
asdf123
abcd123
bfabc123