文本块命令
- 常用指令
- i(insert):插入
- a(append):追加
- r(read):读取文件|导入文件内容
- w(write):文件另存|导出文件内容
- insert(插入,行前写入)
sed '2i ABC_XYZ' test.txt #给第二行前面插入
sed '3i ABC_XYZ' test.txt
sed '/2046/i ABC\nXYZ' test.txt #给所有包含2046行前面插入
sed '/1888/i ABC\nXYZ' test.txt
- 追加指令:append(追加,行后写入)
sed '2a ABC_XYZ' test.txt #给第二行后面插入
sed '3a ABC_XYZ' test.txt
sed '/2046/a ABC\nXYZ' test.txt #给所有包含2046行后面插入
sed '/1888/a ABC\nXYZ' test.txt
- 导入指令:read(将其他文件的内容导入)
sed '2r /etc/hosts' test.txt #将/etc/hosts内容导入到test.txt第二行后面
sed 'r /etc/hosts' test.txt#将/etc/hosts内容导入到test.txt每一行后面
sed '/1888/r /etc/hosts' test.txt #在含有1888的后面导入文件内容
- 导出指令:write(将文件内容导出另存到其他文件)
sed 'w copy_test.txt' test.txt#将test.txt文件的所有内容另存为一个新文件copy_test.txt
sed '/1888/w 1888.txt' test.txt #将test.txt文件中所有包含1888的行另存为新文件1888.txt
sed '2,3w 1888.txt' test.txt#将test.txt的2到3行另存为1888.txt文件
小结
本文我们学习了如何通过sed命令进行数据的插入、追加、导入、导出功能
通过sed的编辑能力,做到活学活用,可以做出很多有意义的事情
网友评论