1. 數(shù)據(jù)準(zhǔn)備
192.168.0.1 test1
192.168.0.3 test3
192.168.0.4 test4
172.115.0.1 tem1
cat ~/test
2. 添加數(shù)據(jù)
在test1 和 test3 之間添加數(shù)據(jù) 192.1168.0.2 test2
sed -i '//a' filename
sed -i '/test1/a 192.1168.0.2 test2' ~/test
3. 刪除數(shù)據(jù)
刪除包含test2的數(shù)據(jù)行
sed -i '/*/d' filename
sed -i '/test2/d' ~/test
sed -i '3d' ~/test # 刪除第三行
sed -i '/^172/d' ~/test # 刪除以172開頭的行
4. 不修改文件數(shù)據(jù)的刪除操作
sed -e '/tem1/d' ~/test # 刪除~/test中含"tem1"的行,但不改變a.txt文件本身,操作之后的結(jié)果在終端顯示
sed -e '/tem1/d' ~/test > a.log # 刪除~/test中含"tem1"的行,將操作之后的結(jié)果保存到a.log
sed '/tem1/d;/test1/d' ~/test > a.log # 刪除含字符串"tem1"或“test1"的行,將結(jié)果保存到a.log
其中,"tem1"也可以用正則表達(dá)式來(lái)代替