第七节 流编辑器sed(stream editor)(7.2.1)

3,2 常用编辑命令
对文件内容的编辑命令说明
d删除匹配的行
p打印匹配到的行
a文本内容将文本内容添加到匹配行的下一行
i文本内容将文本内容添加到匹配行的上一行
c文本内容用文本内容替换匹配到的所有行
r读入文件内容追加到匹配行的后面
R读入文件一行的内容追加到匹配行的后面
w /dir/filename将匹配行的内容另存到/dir/filename中
s /pattern/replacement/flag根据正则表达式进行匹配,将匹配到的内容替换为replacment,
flag可以指定 g(表示全局替换,默认只替换每行第一个),
num1(表示)

示例:

生成测试文件

sed -e '\#adm#,$#:#@@@#g' testfile

a)删除行

sed '1d' testfile
sed '$d' testfile
sed '/root/d' testfile
sed '/root/! d' testfile

b)显示行

sed -n '1p' testfile
sed -n '$p' testfile
sed -n '/root/p' testfile
sed -n '/root/! p' testfile

c)添加,插入,替换行

sed '1ahello world' testfile 
sed '$ahello world' testfile
sed '/adm/ahello world' testfile
sed '/adm/ihello world' testfile
sed '/adm/! ihel1o\nworld' testfile

d)整合文件

vim numfile1
1
2
6
vim numfile2
2
3
4
sed '1rnumfile2' numfile1
sed '1Rnumfile2' numfile1

e)保存处理结果

sed '/root/w file' testfile
cat file
root: x: 0:0: root:/root:/bin/bash

f)字符替换

sed 's/:/##/' testfile
sed 's/:/##/2' testfile 
sed 's/:/##/g' testfile 
sed 's/^./##/g' testfile
sed 's/^\< [a-z]*[a-Z \>//' testfile 
sed 's/^\(...\)\(t\)/\2/g' testfile #元素向后引用
t: x: 0:0: root:/root:/bi n/bash
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星鬼123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值