Linux三剑客之sed的高级用法及应用

sed 基础语法及高级命令

a : 只能插到某一行的后面去

示例:

[root@c2 ~]# cat kk
12345
0.0.0.0
10.10.10.128

[root@c2 ~]# sed '1ahello world' kk
12345
hello world
0.0.0.0
10.10.10.128

如要有空格,要有转义符\
[root@c2 ~]# sed '1a\                    hello world' kk
12345
                    hello world
0.0.0.0
10.10.10.128	


i : 插到某一行的前面去

示例:

[root@c2 ~]# cat kk
12345
0.0.0.0
10.10.10.128	

[root@c2 ~]# sed '1ihello world' kk
hello world
12345
0.0.0.0
10.10.10.128	

可以根据匹配的数字插到他前面一行
[root@c2 ~]# sed '/0.0.0.0/ihello world' kk
12345
hello world
0.0.0.0
10.10.10.128	

c:更改(是把整行都更改)

示例:

[root@c2 ~]# cat kk
12345
0.0.0.0
10.10.10.128	

[root@c2 ~]# sed '2chello' kk
12345
hello
10.10.10.128	

[root@c2 ~]# sed '2,4chello' kk (把他们都替换成hello)
12345
hello

[root@c2 ~]# sed '2,3chello\nworld' kk (可以使用\n换行输出你要输入的文本)
12345
hello
world

如果修改最后一行,使用$字符

[root@c2 ~]# sed '$chello' kk
12345
0.0.0.0
10.10.10.128	
hello

s:替换

g: 全部替换

& : 用正则表达式匹配的内容进行替换

示例:

1.
[root@c2 ~]# echo "hello world tom" | sed -r '/hello/s/tom/&/ '
hello world tom
[root@c2 ~]# echo "hello world tom" | sed -r '/hello/s/tom/1&/ '
hello world 1tom
[root@c2 ~]# echo "hello world tom" | sed -r 's/tom/1&/ '
hello world 1tom
[root@c2 ~]# echo "hello world tom" | sed -r 's/tom/& hello/ '
hello world tom hello

2.
[root@c2 ~]# echo "hello world jjyy" | sed -r 's/hello (.*) (.*)/hello &/g'
hello hello world jjyy

[root@c2 ~]# echo "hello world jjyy" | sed -r 's/hello (.*) (.*)/hello &/g'
hello hello world jjyy

替换元字符

[root@c2 ~]# cat kk
12345
0.0.0.0
10.	10.10.	128	

[root@c2 ~]# sed 's/\t/\  (第二个table换成了换行)
> /2' kk  
12345
0.0.0.0
10.	10.10.
128	

[root@c2 ~]# cat b
.AH "Comment"
.AH "Substitution"
.AH "Delete"
.AH "Change"
.AH "List"
[root@c2 ~]# sed '/.AH/s/.AH/@A /' b
@A  "Comment"
@A  "Substitution"
@A  "Delete"
@A  "Change"
@A  "List"


[root@c2 ~]# sed '/.AH/s/.AH/@A HEAD =/' b
@A HEAD = "Comment"
@A HEAD = "Substitution"
@A HEAD = "Delete"
@A HEAD = "Change"
@A HEAD = "List"

[root@c2 ~]# sed '/.AH/s/.AH/@A HEAD =/;s/\"//g' b (引号替换为空)
@A HEAD = Comment
@A HEAD = Substitution
@A HEAD = Delete
@A HEAD = Change
@A HEAD = List

[root@c2 ~]# cat bbb
.AH "Major Heading"
on the UNIX Operating System.

[root@c2 ~]# sed 's/UNIX/\\s-2&\\s0/' bbb
.AH "Major Heading"
on the \s-2UNIX\s0 Operating System.

”与“ 符号用于在替换字符创中引用整个匹配内容(在正则表达式匹配单词变化有很大作用)

[root@c2 ~]# sed 's/See Section [1-9][0-9]?\.//' bbb
.AH "Major Heading"
on the UNIX Operating System.
See Section 1.4
See Section 12.9
(See Section 12.9)
[root@c2 ~]# sed -r 's/See Section [1-9][0-9]?/(&)/' bbb
.AH "Major Heading"
on the UNIX Operating System.
(See Section 1).4
(See Section 12).9
((See Section 12).9)

交换内容文本,把他们放在转义的圆括号内并在替换过程交换他们

[root@c2 ~]# cat bbb
.AH "Major Heading"
on the UNIX Operating System.
See Section 1.4
See Section 12.9
(See Section 12.9)
second:first
two:one


[root@c2 ~]# sed -r 's/(.*):(.*)/\2:\1/g' bbb
.AH "Major Heading"
on the UNIX Operating System.
See Section 1.4
See Section 12.9
(See Section 12.9)
first:second
one:two

= 等于号: 显示行号

[root@c2 ~]# cat b
.AH "Comment"
.AH "Substitution"
.AH "Delete"
.AH "Change"
.AH "List"
[root@c2 ~]# sed '/^\.AH/=' b
1
.AH "Comment"
2
.AH "Substitution"
3
.AH "Delete"
4
.AH "Change"
5
.AH "List"

q: 会使sed停止读取新的输入行

[root@c2 ~]# sed '2q' kk
12345
0.0.0.0
[root@c2 ~]# cat bbb
.AH "Major Heading"
on the UNIX Operating System.
See Section 1.4
See Section 12.9
(See Section 12.9)
second:first
two:one


[root@c2 ~]# sed '4q' bbb
.AH "Major Heading"
on the UNIX Operating System.
See Section 1.4
See Section 12.9

sed高级命令

n : 1-512之间的一个数,表示对文本模式中指定模式第n次出现的情况进行替换(默认关闭打印功能)

示例:

[root@c2 ~]# cat ttt
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives

[root@c2 ~]# sed  -n '/Operator/p' ttt
Consult Section 3.1 in the Owner and Operator

N: 把自己和下一行放到模式空间

示例:

两行换一行

[root@c2 ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide/g}' ttt
Consult Section 3.1 in the Installation Guide for a description of the tape drives

多行换一行

示例:

[root@c2 ~]# cat ttt
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
[root@c2 ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/Installation Guide/g}' ttt
Consult Section 3.1 in the Installation Guide for a description of the tape drives
Consult Section 3.1 in the Installation Guide for a description of the tape drives
Consult Section 3.1 in the Installation Guide for a description of the tape drives
Consult Section 3.1 in the Installation Guide for a description of the tape drives

多行删除

删除命令:

d : 删除模式空间的内容并导致读入新的输入行,从而在脚本的顶端重新使用编辑方法

将多个空行减少到一行

示例:

[root@c2 test]# cat abc 
This line is followes by 1 blank line.

This line is followes by 2 blank line.


This line is followes by 3 blank line.



This line is followes by 4 blank line.




This is the end.
[root@c2 test]# sed '/^$/{N;/^\n$/d}' abc
This line is followes by 1 blank line.

This line is followes by 2 blank line.
This line is followes by 3 blank line.

This line is followes by 4 blank line.
This is the end.

示例二: 删除1开头的

[root@c2 ~]# cat kk
12345
0.0.0.0
10.10.10.128	
[root@c2 ~]# sed -r '/^1/d' kk
0.0.0.0

D: 删除模式空间中直到第一个嵌入的换行符的这部分内容,他不会导致读入新的输入行,它会返回到脚本的顶端

示例:

[root@c2 test]# cat abc 
This line is followes by 1 blank line.

This line is followes by 2 blank line.


This line is followes by 3 blank line.



This line is followes by 4 blank line.




This is the end.

[root@c2 test]# sed '/^$/{N;/^\n$/D}' abc
This line is followes by 1 blank line.

This line is followes by 2 blank line.

This line is followes by 3 blank line.

This line is followes by 4 blank line.

This is the end.

多行打印

p: 整个模式空间全部打印(自带打印功能)

示例:

[root@c2 ~]# cat b
.AH "Comment"
.AH "Substitution"
.AH "Delete"
.AH "Change"
.AH "List"

[root@c2 ~]# sed 'p' b
.AH "Comment"
.AH "Comment"
.AH "Substitution"
.AH "Substitution"
.AH "Delete"
.AH "Delete"
.AH "Change"
.AH "Change"
.AH "List"
.AH "List"

P: 输出多行模式空间的第一部分,直到第一个嵌入的换行符为止(打印模式空间的第一行)

经常出现在Next命令之后和delete命令之前

示例:

[root@c2 ~]# cat c
Herer are example of the UNIX
System.
Where UNIX
System appears,it should be the UNIX
Operating System.

[root@c2 ~]# sed '/UNIX/{N;/\nSystem/{s// Operating &/;P}}' c
Herer are example of the UNIX Operating (在这里加上了Operating)
Herer are example of the UNIX Operating 
System. 
Where UNIX Operating 
Where UNIX Operating 
System appears,it should be the UNIX
Operating System.

如加上D,效果如下
[root@c2 ~]# sed '/UNIX/{N;/\nSystem/{s// Operating &/;P;D}}' c
Herer are example of the UNIX Operating 
System.
Where UNIX Operating 
System appears,it should be the UNIX
Operating System.

模式空间和保持空间

  • 模式空间是容纳当前输入行的缓冲区.
  • 模式空间的内容可以复制到保持空间,保持空间的也可以复制到模式空间.
  • 还有一个称为保持空间的顶流(set-aside)缓冲区.
保持空间的用途:
  • 用于临时存储
  • 保留当前输入行的副本
命令:
命令缩写功能
Holdh 或 H将模式空间的内容复制或追加到保持空间
Getg 或 G将保持空间的内容复制或追加到模式空间
Exchangex交换保持空间和模式空间的内容

示例:

[root@c2 ~]# cat d
1 
2
11
22
111
222


[root@c2 ~]# sed '/1/{h;d};/2/{G}' d
2
1 
22
11
222
111

[root@c2 ~]# sed '/1/{h;d};/2/{g}' d (g 会覆盖)
1 
11
111


大写转换:

y : 字母大小写转换

示例:

[root@c2 ~]# cat cc
find the Match statement
Consult the Get statement
Using the Read statement to retrieve data


[root@c2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;y/abcdefghijkomnopqrstuvwxyz/ABCDEFGHIJKOMNOPGRSTUVWXYZ/;G}' cc
MATCH
find the Match statement
GET
Consult the Get statement
READ
Using the Read statement to retrieve data

[root@c2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;y/abcdefghijkomnopqrstuvwxyz/ABCDEFGHIJKOMNOPGRSTUVWXYZ/;G;s/(.*)\n(.*the ).*( statement.*)/\2\1\3/g}' cc
find the MATCH statement
Consult the GET statement
Using the READ statement to retrieve data


示例:

[root@c2 ~]# cat yy
ffffffffffjj red hehe hat jjyy
jjdhsdcdjfjf red xixi hat
dhsjbcaj red haha hat hello world hhuu


[root@c2 ~]# sed -r '/red .* hat/{h;s/.*red (.*) hat.*/\1/;y/abcdefghijkomnopqrstuvwxyz/ABCDEFGHIJKOMNOPQRSTUVWSYZ/;G;s/(.*)\n(.* red ).*( hat.*)/\2\1\3/}' yy
ffffffffffjj red HEHE hat jjyy
jjdhsdcdjfjf red SISI hat
dhsjbcaj red HAHA hat hello world hhuu
[root@c2 ~]# 

构建文本块

保持空间可用于在输出行块之前收集它们。

一些troff请求和宏是面向块的,他必须包围文件块。

”, 分别用于开始和结束一个段落。
[root@c2 ~]# cat yy
ffffffffffjj red hehe hat jjyy
jjdhsdcdjfjf red xixi hat
dhsjbcaj red haha hat hello world hhuu

[root@c2 ~]# sed '/^$/!{H;d}' yy ### 删除模式空间
[root@c2 ~]# 


[root@c2 ~]# cat yy
<p>ffffffffffjj red hehe hat jjyy</p>

<p>jjdhsdcdjfjf red xixi hat</p>

<p>dhsjbcaj red haha hat hello world hhuu</p>
[root@c2 ~]# sed '/^$/!{H;d};/^$/{x;s/^\n/<p>/;s/$/<\/p>/;G}' yy
<p>ffffffffffjj red hehe hat jjyy</p>

<p>jjdhsdcdjfjf red xixi hat</p>

<p>dhsjbcaj red haha hat hello world hhuu</p>


高级的流控制命令

分支b和测试t 命令将脚本中的控制转移到包含特殊标签的行。

如果没有指定标签,则将控制转移到脚本的结尾处。

分支命令用于无条件转移,测试命令用于有条件转移,他们只有当替换命令改变当前行时才会执行。

标签是任意不多于7个字符的序列。标签本身占据一行并以冒号开始:

:mylabel

在冒号和标签之间不允许有空格。行结尾处的空格将被认为是标签的一部分。

当在分支命令或测试命令中指定标签时,在命令和标签之间允许有空格:

b my |abe|

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值