基本sed命令

基本sed命令

sed的基本命令

  • i:插入命令,类似于 a 命令,但不是在当前行后增加文本,而是在当前行前面插入新的文本,即刚读入缓存区模式的行。
  • a:追加将新文本到文件中当前行(即读入模式的缓存区行)的后面
  • p:是打印命令,用于显示模式缓存区的内容
  • r:从文件中读取输入行
  • s:用一个字符串替换另一个
  • h: 把模式空间中的内容覆盖至保持空间中
  • H:把模式空间中的内容追加至保持空间中
  • g: 从保持空间取出数据覆盖至模式空间
  • G:从保持空间取出内容追加至模式空间
  • x: 把模式空间中的内容与保持空间中的内容进行互换
  • n: 读取匹配到的行的下一行覆盖至模式空间,打印模式空间的值,并读取下一行。
  • N:读取匹配到的行的下一行追加至模式空间,它不打印模式空间的值
  • d: 删除模式空间中的行
  • D:如果模式空间包含换行符,则删除直到第一个换行符的模

sed命令语法

单个行地址

[address]command  

使用大括号进行分组使其用于同一个地址

[line-address]command
替换

c : 用新文本修改(替换)当前行中的文本
替换命令语法

[address]s/pattern/replacement/flages

这里修饰替换的标志flags是:

  • n :1到512之间的数字,表示文本模式第n次出现的情况进行替换
  • g :对匹配的结果进行全局替换
  • p :打印替换后的内容
  • w :将模式空间的内容写到文件file中
[root@localhost ~]# echo '/usr/local/src' | sed 's#/usr/local/src#/user/local/src#'
/user/local/src

在这里插入图片描述

[root@localhost ~]# cat abc 
column1 column2 column3 column4  
[root@localhost ~]# sed 's/\t/*/'2 abc  //使用\t代表制表符
column1 column2*column3 column4

替换元字符
替换元字符是反斜杠() “与”符号(&)和\n。

反斜杠一般用于转义其他的元字符,但是他在替换字符串中也用于包含换行符(反斜杠后不允许有空格)

用换行符取代每行上的第二个字符

[root@localhost ~]# sed 's#\t#\              
#2' abc
column1 column2
column3 column4

[root@localhost ~]# sed 's#\t#\n#2' abc
column1 column2
column3 column4
[root@localhost ~]# cat 123 
.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' 123  
.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 
123

123

123
[root@localhost ~]# sed '/^$/d' test 
123
123
123

删除一个完整的行

[root@localhost ~]# cat 123
.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' 123
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

追加

a : 在当前行后添加一行或多行内容

[root@localhost ~]# cat 123
.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 '1a abc' 123
.Ah Major Heading
abc
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

添加空格(空格需要引起来)

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

在匹配模式的每一行之后追加一行,在"See"后面追加

[root@localhost ~]# sed '/^See/a 123' 123   
.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]/a abc' 123
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
abc
first:second
one:two
插入

i : 在当前行之前插入文本 (删除文件sed -i.bak; -i参数必须写在所有参数最后面)
单行追加

[root@localhost ~]# cat 123
.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 '1i  xixi' 123 
xixi
.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/i xixi' 123  
.Ah Major Heading
xixi
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two

多行追加

[root@localhost ~]# cat 123 
.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 '/^first/i xixi\   
hehe' 123
.Ah Major Heading
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
xixi
hehe
first:second
one:two
更改

c : 用新文本修改(替换)当前行中的文本

[root@localhost ~]# cat 123
.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/,/on/c abc' 123   
abc
See Section 1.4
See Section 12.9
first:second
one:two
转换

y : 将字符替换成一个新的字符 (替换文本中相同的字符)

[root@localhost ~]# cat 123
.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 'y/ajor/1234/' 123
.Ah M1234 He1ding
ORA Ass3ci1tes, Inc.
3n the UNIX Ope41ting System
See Secti3n 1.4
See Secti3n 12.9
fi4st:sec3nd
3ne:tw3

转换单行中的字符

[root@localhost ~]# sed '1y/ajor/1234/' 123
.Ah M1234 He1ding
ORA Associates, Inc.
on the UNIX Operating System
See Section 1.4
See Section 12.9
first:second
one:two
打印

**p : 打印文本 **

[root@localhost ~]# cat abc 
.Ah "Comment"
.Ah "Substitution"
.Ah "Delete"
[root@localhost ~]# sed '/^\.Ah/{p}' abc 
.Ah "Comment"
.Ah "Comment"
.Ah "Substitution"
.Ah "Substitution"
.Ah "Delete"
.Ah "Delete"

在行改变之前和之后打印

[root@localhost ~]# sed '/^\.Ah/{p;s/"//g;s/\.Ah/xixi/}' abc  
.Ah "Comment"
xixi Comment
.Ah "Substitution"
xixi Substitution
.Ah "Delete"
xixi Delete

打印行数

[root@localhost ~]# sed -n '/Comment/{=;p}' abc  
1
.Ah "Comment"

sed高级命令

多行模式空间
追加下一行

N:读取匹配到的行的下一行追加至模式空间,它不打印模式空间的值

[root@localhost ~]# cat abc
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.


[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' abc
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.


[root@localhost ~]# sed -n '/Operator$/{N;p}' abc  //匹配以Operator结尾的行 N和匹配行的下一行
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives


[root@localhost ~]# sed -n '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g ;p}' abc   //将Owner and Operator\nGuide 变成 installation Guide   
Consult Section 3.1 in the installation Guide for a description of the tape drives  

[root@localhost ~]# cat abc
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 inc luding the Owner and
Operator Guide and the User Gui de.
The Owner and Operator Guide is shipped with your system.


[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g}' abc
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 inc luding the installation Guide
and the User Gui de.
The installation Guide is shipped with your system.




[root@localhost ~]# sed 's/Owner and Operator Guide/installation Guide/g' abc   //将Owner and Operator Guide替换为installation Guide
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 inc luding the Owner and
Operator Guide and the User Gui de.
The installation Guide is shipped with your system.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/p' abc 
Consult Section 3.1 in the Owner and Operator
Two manuals are provided inc luding the Owner and


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;p}' abc 
Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and
Operator Guide and the User Gui de.


[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;p}' abc  
Consult Section 3.1 in the Owner and Operator Guide for a description of the tape drives
Two manuals are provided inc luding the Owner and Operator Guide and the User Gui de.

[root@localhost ~]# sed -n 's/Owner and Operator Guide/installation Guide/g;/Owner/{N;s/ *\n/ /g;s/Owner and Operator Guide */installation Guide\n/g;p}' abc  //将Owner and Operator Guide * 替换为installation Guide\n 
Consult Section 3.1 in the installation Guide
for a description of the tape drives
Two manuals are provided inc luding the installation Guide
and the User Gui de.

多行删除

D:如果模式空间包含换行符,则删除直到第一个换行符的模

[root@localhost ~]# cat abc
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 b lank lines.




This is the end.

[root@localhost ~]# sed  '/^$/{N;/^\n$/d}' abc
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 b lank lines.
This is the end.

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

This line is followed by 2 blank lines.

This line is followed by 4 blank lines.
多行打印

P:如果模式空间包含换行符,则打印直到第一个换行符的模

[root@localhost ~]# cat abc
Here are examples of the UNIX
System. Where UNIX
System appears,it should be the UNIX
Operating System.

[root@localhost ~]# sed -n '/UNIX$/p' abc 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX

[root@localhost ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D}}' abc
Here are examples of the UNIX Operating 
System. Where UNIX Operating 
System appears,it should be the UNIX
Operating System.

[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D;p}}' abc 
Here are examples of the UNIX Operating 
System. Where UNIX Operating 

包含那一行
命令缩写功能
Holdh或H将模式空间的内容复制或追加到保持空间
Getg或G将保持空间的内容复制或追加到模式空间
Exchangex交换保持空间和模式空间的内容
[root@localhost ~]# cat abc
1
2
11
22
111
222


[root@localhost ~]# sed -n '/1/{h;d};/2/{G};p' abc
2
1
22
11
222
111

[root@localhost ~]# sed -n '/1/{h;d};p' abc
2
22
222
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值