sed基础命令

sed命令


1.什么是sed

sed对文本的处理很强大,并且sed非常小,参数少,容易掌握,他的操作方式根awk有点像。sed按顺序逐行读取文件。然后,它执行为该行指定的所有操作,并在完成请求的修改之后的内容显示出来,也可以存放到文件中。完成了一行上的所有操作之后,它读取文件的下一行,然后重复该过程直到它完成该文件。在这里要注意一点,源文件(默认地)保持不被修改。sed 默认读取整个文件并对其中的每一行进行修改。说白了就是一行一行的操作。我用sed主要就是用里面的替换功能,真的很强大。下面以实例,详细的说一下,先从替换开始,最常用的。

2.sed命令

// n --读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
// N --追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。
// h --拷贝模式空间的内容到内存中的保持空间,拷贝会覆盖原内容。
// H --追加模式空间的内容到保持空间。
// g --拷贝保持空间的内容到模式空间,拷贝会覆盖原内容。
// G --追加保持空间的内容到模式空间。
// x --交换保持空间和模式空间的内容。
// p --打印模式空间的行。
// P --打印模式空间的第一行。
// d --删除模式空间选择的行。
// D --删除模式空间的第一行。
// y --将字符替换为另一个字符。
// a --在当前行的下面插入文本。
// i --在当前行的上面插入文本。
// c --将选择的文本替换为新的文本。

3.sed命令使用示例。

// n --读取下一个输入行,用下一个命令处理新的行而不是用第一个命令。
[root@localhost tmp]# cat a		
Consult Section 3.1 in the Owner and Operator
Guide for a descr iption of the tape drives
available on your system.
将包含Operator的下一行的Guide for替换为Fuide for
[root@localhost tmp]# sed '/Operator/{n;s/Guide for/Fuide for/g}' a   
Consult Section 3.1 in the Owner and Operator
Fuide for a descr iption of the tape drives
available on your system.

// N --追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码。
[root@localhost tmp]# cat a
Consult Section 3.1 in the Owner and Operator
Guide for a descr iption of the tape drives
available on your system.
//
[root@localhost tmp]# sed '/Operator$/{N;s/Owner and Operator\nGuide /Installation Guide\n/}' a
Consult Section 3.1 in the Installation Guide
for a descr iption of the tape drives
available on your system.

// h --拷贝模式空间的内容到内存中的保持空间,拷贝会覆盖原内容。
[root@localhost tmp]# cat 4
1
2
11
22
111
222
[root@localhost tmp]# sed '/1/{h;d}' 4	//1,11,111复制到保持空间,然后删除模式空间的内容
2
22
222
// H --追加模式空间的内容到保持空间。
[root@localhost tmp]# cat 4
1
2
11
22
111
222
[root@localhost tmp]# sed '/1/{H;d};/2/{g}' 4

1

1
11

1
11
111

// g --拷贝保持空间的内容到模式空间,拷贝会覆盖原内容。
[root@localhost tmp]# cat 4
1
2
11
22
111
222
[root@localhost tmp]# sed '/1/{H;d};/2/{g}' 4

1

1
11

1
11
111

// G --追加保持空间的内容到模式空间。
[root@localhost tmp]# cat 4
1
2
11
22
111
222
//将保持空间的内容追加在模式空间来
[root@localhost tmp]# sed  '/[12]/{h;G}' 4
1
1
2
2
11
11
22
22
111
111
222
222


// p --打印模式空间的行。
[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
[root@localhost tmp]# sed 'p' 2	//sed命令默认打印一遍p命令打印一遍,所以就显示两遍
See Section 1.4
See Section 1.4
See Section 12.9
See Section 12.9

[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
[root@localhost tmp]# sed -n '2p' 2
See Section 12.9
//-n选项取消自动打印,然后指定打印的行数
// P --打印模式空间的第一行。
[root@localhost tmp]# cat 3
Here are examples of UNIX
System. Where UNIX
System appears,it showld be the UNIX
Operating System.
[root@localhost tmp]# sed '/UNIX$/{N;/\nSystem/s// Operating &/;P;D}' 3
Here are examples of UNIX Operating
System. Where UNIX Operating
System appears,it showld be the UNIX
Operating System.

// d --删除模式空间选择的行
[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
adas
das
das
d
[root@localhost tmp]# sed '/1.4$/{d;p}' 2
See Section 12.9
adas
das
das
d

// D --删除模式空间的第一行。
[root@localhost tmp]# cat 2
See Section 1.4

See Section 12.9



adas


das



das




d
[root@localhost tmp]# sed '/^$/{N;/^\n$/D}' 2
See Section 1.4

See Section 12.9

adas

das

das

d

// y --将字符替换为另一个字符。
[root@localhost tmp]# cat 1
11
22
33
44
55
11
[root@localhost tmp]# sed '6y/11/66/' 1
11
22
33
44
55

// a --在当前行的下面插入文本。
[root@localhost tmp]# cat 1
11
22
33
44
55
11
[root@localhost tmp]# sed '/55/a\66' 1
11
22
33
44
55
66
11
// i --在当前行的上面插入文本。
[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
[root@localhost tmp]# sed '1i123' 2
123
See Section 1.4
See Section 12.9


// c --将选择的文本替换为新的文本。
[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
[root@localhost tmp]# sed '2chuangweifeng' 2
See Section 1.4
huangweifeng

//以12.9结尾的行,改为添加4个空格,添加空格要在第一个空格前添加一个\转义符
[root@localhost tmp]# cat 2
See Section 1.4
See Section 12.9
[root@localhost tmp]# sed '/12.9$/c\    huangweifeng' 2
See Section 1.4
    huangweifeng
[root@localhost tmp]# sed '2chuangweifeng' 2
See Section 1.4
huangweifeng

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

1we11

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

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

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

打赏作者

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

抵扣说明:

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

余额充值