sed高级应用

12 篇文章 0 订阅

sed高级应用

命令
a在当前行下面插入文本
i在当前行上面插入文本
c把选定的行改为新的文本
d删除选择的行
D删除模式空间的第一行
y表示把一个字符翻译为另外的字符(但是不用于正则表达式)
n读取下一个输入行,用下一个命令处理新的行而不是用第一个命令
N追加下一个输入行到模式空间后面并在二者间嵌入一个新行,改变当前行号码
p打印模式空间的行
P(大写)打印模式空间的第一行
g获得保持空间的内容,并替代当前模式空间中的文本
G获得保持空间的内容,并追加到当前模式空间文本的后面
h拷贝模式空间的内容到内存中的保持空间
H追加模式空间的内容到内存中的保持空间
x表示互换模式空间中的文本和保持空间中的文本


1. sed基本命令

# 删除第3行数据
[root@localhost ~]# cat test_1 
123
456
789
321
654
987

[root@localhost ~]# sed '3d' test_1 
123
456
321
654
987

转义符可以是 “\”“#”、或“!”

[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src/#'
/user/local/src/

\t表示制表符,将制表符替换为>

[root@localhost ~]# echo 'Column1 Column2 Column3 Column4' > test_1 
[root@localhost ~]# cat test_1 
Column1 Column2 Column3 Column4

[root@localhost ~]# sed 's/\t/>/2' test_1 
Column1	Column2>Column3	Column4

\n表示换行

[root@localhost ~]# sed 's/\t/\n/2' test_1 
Column1	Column2
Column3	Column4

.Ah "Major Heading"替换为@A HEAD = Major Heading

[root@localhost ~]# cat test_2
.Ah "Major Heading"

# 第一种方法:
[root@localhost ~]# sed '/^\.Ah/{
s/\.Ah */\
\
@A HEAD = /
s/"//g
s/$/\
/
} ' test_2

@A HEAD = Major Heading

# 第二种方法:
[root@localhost ~]# sed '/^\.Ah/{s/\.Ah */@A HEAD = /;s/"//g}' test_2
@A HEAD = Major Heading

输出效果为:O' Reilly ORA Associates, Inc.

-r表示用正则表达式

[root@localhost ~]# echo 'ORA Associates, Inc.' >> test_2
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.

[root@localhost ~]# sed -r "s/ORA (.*)/O' Reilly &/g" test_2
.Ah "Major Heading"
O' Reilly ORA Associates, Inc.

输出效果为:on the \s-2UNIX\s0 Operating System.

[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.

[root@localhost ~]# sed 's/UNIX/\\s-2&\\s0/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the \s-2UNIX\s0 Operating System.

“&”可以引用匹配的内容

[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9

[root@localhost ~]# sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
(See Section 1.4)
(See Section 12.9)

[root@localhost ~]# sed -r 's/(See Section) ([1-9][0-9]*\.[1-9][0-9]*)/\1\\fB\2\\fp/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section\fB1.4\fp
See Section\fB12.9\fp

交换内容位置

[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed -r 's/(.*):(.*)/\2:\1/g' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
second:first
two:one

删除内容(d)
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed '/^.Ah/d' test_2
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

追加(a)
[root@localhost ~]# sed '1aabc' test_1
Column1	Column2	Column3	Column4
abc

[root@localhost ~]# sed '1a abc' test_1
Column1	Column2	Column3	Column4
abc

[root@localhost ~]# sed '/^See/a 123' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
123
See Section 12.9
123
first:second
one:two

[root@localhost ~]# sed '/See.*[1-9][0-9]\.[0-9]/a123' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
123
first:second
one:two

插入(i)
[root@localhost ~]# sed '1ihello' test_1
hello
Column1	Column2	Column3	Column4

[root@localhost ~]# sed '1 i hello' test_1
hello
Column1	Column2	Column3	Column4

[root@localhost ~]# sed '/^\.Ah/ihello' test_2
hello
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

更改(c)
[root@localhost ~]# cat test_1
Column1	Column2	Column3	Column4

[root@localhost ~]# sed '1chello' test_1
hello

[root@localhost ~]# sed '1chello' test_1
hello
[root@localhost ~]# cat test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed '/ORA/c \hello' test_2
.Ah "Major Heading"
hello
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

[root@localhost ~]# sed '/^\.Ah/,/on /chello' test_2
hello
See Section 1.4
See Section 12.9
first:second
one:two

转换(y)
[root@localhost ~]# sed '1y/ajo/123/' test_2
.Ah "M123r He1ding"
ORA Associates, Inc.
on the UNIX Operating System.
See Section 1.4
See Section 12.9
first:second
one:two

下一行(n)
# 在匹配到内容的下一行进行更改
[root@localhost ~]# sed -r '/Associates/{n;s/on the (.*)/on the (\1)/g}' test_2
.Ah "Major Heading"
ORA Associates, Inc.
on the (UNIX Operating System.)
See Section 1.4
See Section 12.9
first:second
one:two


2. 多行模式空间

追加下一行(N)
[root@localhost ~]# cat test 
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.
[root@localhost ~]# sed -n '/Operator$/{N;p}' test 
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
[root@localhost ~]# sed '/3.1/{N;s/3.1/3.2/g;s/Owner and Operator\nGuide/installation Guide/g}' test 
Consult Section 3.2 in the installation Guide for a description of the tape drives
available on your system.
[root@localhost ~]# cat test 
Consult Section 3.1 in the Owner and Operator 
Guide for a description of the tape drives 
available on your system.
Look in the Owner and Operator Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Owner and Operator Guide is shipped with your system.
[root@localhost ~]# sed 's/Owner and Operator Guide/Installation Guide/g' test 
Consult Section 3.1 in the Owner and Operator 
Guide for a description of the tape drives 
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Owner and
Operator Guide and the User Guide.
The Installation Guide is shipped with your system.
[root@localhost ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/p' test 
Consult Section 3.1 in the Owner and Operator 
Two manuals are provided including the Owner and
[root@localhost ~]# sed -n 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;p}' test 
Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives 
Two manuals are provided including the Owner and Operator Guide and the User Guide.
[root@localhost ~]# sed 's/Owner and Operator Guide/Installation Guide/g;/Owner/{N;s/ *\n/ /;s/Owner and Operator Guide */Installation Guide\n/g}' test 
Consult Section 3.1 in the Installation Guide
for a description of the tape drives 
available on your system.
Look in the Installation Guide shipped with your system.
Two manuals are provided including the Installation Guide
and the User Guide.
The Installation Guide is shipped with your system.

多行删除(D)
[root@localhost ~]# cat test 
This line is followed by 1 blank line.

This line is followed by 2 blank lines.


This line is followed by 3 blank lines.



This line is followed by 4 blank lines.




[root@localhost ~]# sed -n '/^$/{N;/^\n$/d;p}' test 

This line is followed by 2 blank lines.

This line is followed by 4 blank lines.

[root@localhost ~]# sed '/^$/{N;/^\n$/d}' test 
This line is followed by 1 blank line.

This line is followed by 2 blank lines.
This line is followed by 3 blank lines.

This line is followed by 4 blank lines.


多行打印(P)
[root@localhost ~]# cat test 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
[root@localhost ~]# sed -n '/UNIX$/p' test 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;p}' test 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{p}}' test 
Here are examples of the UNIX
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' test 
Here are examples of the UNIX Operating 
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D;p}}' test 
Here are examples of the UNIX Operating 
System. Where UNIX Operating 

替换&追加(g、G)
[root@localhost ~]# cat test 
1
2
11
22
111
222
[root@localhost ~]# sed '/1/{h;d};/2/G' test 
2
1
22
11
222
111
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值