sed用法,通常p会与参数sed


sed用法


目录
  • sed用法
    • 基础sed命令
      • sed命令的基本语法
        • sed命令选项
    • 高阶sed命令
      • 模式空间与保持空间
        • sed命令选项

基础sed命令

sed命令的基本语法

sed OPTIONS… [SCRIPT] [INPUTFILE…]

常用的选项:

-n,–quiet: 不输出模式空间中的内容

-i: 直接编辑原文件,默认不对原文件进行操作

-e: 可以使用多个命令(脚本)进行操作

-f /path/from/sed_script: 从指定的文本中读取处理脚本

-r: 使用扩展正则表达式

sed命令选项

替换标记
g:表示行内全面替换
w:表示把行写入一个文件
x:表示互换模式空间的文本和保持空间的文本
y:表示把一个字符翻译为另外的字符(不用于正则表达式)
单行模式空间
a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)

c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!

d :删除,因为是删除,所以 d 后面通常不接任何东西;

i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);

p :打印,即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行

s :取代,通常这个 s 的动作可以搭配正则表达式!例如 1,20s/old/new/g

n:读取下一个输入行, 用下一个命令处理新的行

y:把一个或多个字符替换成另一个字符
a的用法

[root@localhost ~]# vim xbz
[root@localhost ~]# cat xbz
a b c
d
c
b
a
[root@localhost ~]# sed '3abbxxxx' xbz //在第三行下面(第四行)进行新增
a b c
d
c
bbxxxx
b
a
[root@localhost ~]# sed '/c/abbxxxx' xbz  //在匹配的参数(c)下一行进行添加
a b c
bbxxxx
d
c
bbxxxx
b
a

c的用法


[root@localhost ~]# cat xbz 
a b c
d
c
b
a
[root@localhost ~]# sed '2cxxb' xbz //取代第二行
a b c
xxb
c
b
a
[root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed '/d/caa' xbz  //在匹配的参数(d)进行取代
a b c
aa
c
b
a

d的用法

root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed '1d' xbz //删除第一行
d
c
b
a

[root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed '/c/d' xbz //在匹配的参数(c)进行整行删除
d
b
a

i的用法

[root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed '2i3838' xbz  //在第二行进行插入
a b c
3838
d
c
b
a

[root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed '/c/i6868' xbz //在匹配的参数(c)那一行进行插入
6868
a b c
d
6868
c
b
a

p的用法

[root@localhost ~]# cat xbz 
a b c
d
c
b
a

[root@localhost ~]# sed -n '/b/p' xbz //-n选项:只显示匹配处理的行(否则会输出所有)(也就是关闭默认的输出),只是打印带b的行
a b c
b

s的用法

[root@localhost ~]# cat xbz 
a b c
d
c
bbb
a

[root@localhost ~]# sed 's/b/a/' xbz //将匹配的参数(b)每行里的第一个参数进行替换
a a c
d
c
abb
a
[root@localhost ~]# cat xbz 
a b c
d
c
bbb
a

[root@localhost ~]# sed 's/b/a/g' xbz //在上面的基础是加上g就可以全部进行替换
a a c
d
c
aaa
a

n的用法
此处的n不是sed -n的n的那种用法,是n读取下一个输入行

[root@localhost ~]# cat xbz 
a b c
d
c
bbb
a

[root@localhost ~]# sed -n '/a/n;p' xbz //匹配到的参数(a)下面的所有行
d
c
bbb


y的用法

[root@localhost ~]# cat xbz 
a b c
d
c
bbb
a
[root@localhost ~]# sed '3y/c/C/' xbz //将匹配到的第三行小写c改为大写C
a b c
d
C
bbb
a

高阶sed命令

模式空间与保持空间

模拟空间:
当前处理输出的缓冲空间,因为sed就是一次处理一行的内容,就会把这一行的内容提取到模式空间,然后用sed命令处理这一行的内容,处理完成后输出到屏幕,接着处理下一行 的内容
保持空间:
保持空间就是sed的另一个缓冲区,此缓冲区如其名,不会自动清空内容,也不会把缓冲区的内容打印到的标准输出中
模式空间与保持空间的关系
模式空间:相当于流水线,文本行再模式空间中进行处理;
保持空间:相当于仓库,在模式空间对数据进行处理时,可以把数据临时存储到保持空间;作为模式空间的一个辅助临时缓冲区,但又是相互独立,可以进行交互,命令可以寻址模式空间但是不能寻址保持空间。可以使用高级命令h,H,g,G与模式空间进行交互。

sed命令选项

多行空间模式
N:读取匹配到的行的下一行追加至模式空间
P:打印模式空间开端至\n内容,并追加到默认输出之前
D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本, 并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间 不包含换行符,则会像发出d命令那样启动正常的新循环
N追加下一行

[root@localhost ~]# cat xbz.txt 
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}' xbz.txt 
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}' xbz.txt 
Consult Section 3.1 in the installation Guide for a description of the tape drives
[root@localhost ~]# sed '/Operator$/{N;s/Owner and Operator\nGuide/installation Guide/g}' xbz.txt 
Consult Section 3.1 in the installation Guide for a description of the tape drives
available on your system.
//我们假设想要将“Owner and 0perator Guide”换成“lnstallation Guide”,但是我们发现它出现在文件中的两行上,“Operator”和“Guide”被分开了。
Owner and Operator Guide 换成 installation Guide
空格用\n

D多行删除

[root@localhost ~]# cat test

This is the header line.
This is a data line.

This is the last line.
[root@localhost ~]# sed '/^$/{N ; /header/D}' test //删除模式空间的第一行
This is the header line.
This is a data line.

This is the last line.

P多行打印

[root@localhost ~]# cat xxb 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
Operating System.
[root@localhost ~]# sed -n '/UNIX$/p' xxb 
Here are examples of the UNIX
System. Where UNIX
System appears, it should be the UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;p}' xxb 
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}}' xxb
Here are examples of the UNIX
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;p}}' xxb
Here are examples of the UNIX Operating 
System. Where UNIX
[root@localhost ~]# sed -n '/UNIX$/{N;/\nSystem/{s// Operating &/g;P;D;p}}' xxb
Here are examples of the UNIX Operating 
System. Where UNIX Operating 

保持空间

命令 缩写 功能
Hold h(复制)或H (追加) 上传 将模式空间的内容复制或追加到保持空间
Get g或G下载 将保持空间的内容复制或追加到模式空间
Exchange x 交换保持空间和模式空间的内容
[root@localhost ~]# cat abc
1
2
11
22
111
222
[root@localhost ~]# sed '/1/{h;d};/2/G' abc //匹配1将内容放入保持空间,删除,在将匹配2的内容追加模式空间
2
1
22
11
222
111

相关内容