linux下的sed使用

 sed介绍

sed 是一款流编辑工具,针对文件进行过滤、替换操作,在大批量的场景下相当实用。文本可来自于本地文件,标准输入(包括键盘输入,文件重定向,字符串,变量,管道文件)等。

 sed 使用场景

1,编辑相对交互式编辑器而言太大的文件

2,编辑命令过于复杂,在交互式文本编辑器中难以输入的情况

3,对文件扫描一遍,但是需要执行多个编辑函数的情况

 sed工作流程

文件 >>(模式空间)sed指令集  >> 显示屏幕/重定向输出

sed 通过一次仅读一行内容来对某些指令进行处理后输出(适合大数据文件),sed在文件中读取数据,若没有文件,则默认标准输入进行处理。

首先,sed通过文件或者管道读取文件内容,但sed不会直接修改源文件,而是将数据读入的内容复制到缓冲区,这部分缓冲区称之为模式空间(pattern),所有的指令操作都在模式空间进行,然后sed根据相应的指定模式空间进行相应输出,默认情况下为标准输入(打印到屏幕)。

sed基本语法

格式: sed [选项] [sed命令|脚本命令] [输入文件] 

选项】:

 --version  显示版本信息

 -- help  显示帮助信息

-n  --quit , --silent   静默输出,默认情况下sed程序在所有的脚本指令行执行完毕后,将自动打印模式空间的内容,加上-n就不会打印所有行到标准输出,只会打印匹配的内容

-e  script  允许多个脚本指令被执行

-f  script-file  从文件中读取脚本指令

-i[SUFFIX] , --in-place[--SUFFIX]  将修改内容直接修改为源文件(慎用)

-r  在脚本中使用扩展正则表达式

-s ,--separate   默认情况下,sed将输入的多个文件作为一个长的连续的输入流,而GNUsed则把他们当做单个文件

sed命令】:

由 定位文本行+sed编辑命令 两个部分组成

定位文本方法

x  x指定行号

x,y 指定x-y行范围

/pattern/  查看匹配所包含的行

/pattern/pattern2/  查看包含两个匹配的行

/pattern/,x   查看pattern 到x的行

x,/pattern/  从x行到pattern所包含的行

x,y!   不包含x,y 行

sed编辑命令

p 打印匹配的行

= 打印文件行号

a\ 在定位行号之后追加文本信息

i\ 在定位行号之前插入文本信息

d 删除定位符

c\ 替换定位文本

s  使用替换模式替换相应模式

r  从另一个文本中读入文本

w 将文本写入到一个文件

y 变换字符

q  第一个模式匹配完成后退出

g  将保持缓冲区的内容复制到模式缓冲区

G 将保持缓冲区的内容追加到模式缓冲区

示例

打印范围:

显示第二行的内容

[root@centos7 ~]# sed -n '2p' sedpractice.txt

显示1-3行的内容

[root@centos7 ~]# sed -n '1,3p' sedpractice.txt 

显示从1行开始,每隔3行打印

[root@centos7 ~]# sed -n '1~3p' sedpractice.txt

打印最后一行

[root@centos7 ~]# sed -n '$p' sedpractice.txt

匹配第1行及后面的2行内容

[root@centos7 ~]# sed -n '1,+2p' sedpractice.txt

打印匹配模式:

匹配test的行

[root@centos7 ~]# sed -n '/test/p' sedpractice.txt

匹配第2行到 含有wq的行,非贪婪匹配

[root@centos7 ~]# sed -n '2,/wq/p' sedpractice.txt

匹配含有test到含有wq的行

[root@centos7 ~]# sed -n '/test/,/wq/'p sedpractice.txt

匹配第1行到最后一行

[root@centos7 ~]# sed -n '1,$'p sedpractice.txt

匹配含有$的行

[root@centos7 ~]# sed -n '/\$/'p sedpractice.txt

显示test的行号

sed -n -e '/test/='  sedpractice.txt

显示test的行号和内容

sed -n -e '/test/=' -e '/test/p'  sedpractice.txt

文本追加和插入

可以指定文本一行或多行附加,如果不指定位置,则会加在每一行的后面

第2行后面追加hello

[root@centos7 ~]# sed '2a\hello'  sedpractice.txt

第2行前面插入hello

[root@centos7 ~]# sed '2i\hello'  sedpractice.txt

在匹配到test行后面追加hello

[root@centos7 ~]# sed '/test/a\word'  sedpractice.txt

删除

删除第1行

[root@centos7 ~]# sed '1d'  sedpractice.txt

删除最后一行

[root@centos7 ~]# sed '$d'  sedpractice.txt

删除匹配到test的行

sed '/test/d'  sedpractice.txt

修改或替换

行替换

第一行替换为nonono

[root@centos7 ~]# sed '1c\nonono'  sedpractice.txt

匹配到test的行替换为nonono

[root@centos7 ~]# sed '/test/c\nonon'  sedpractice.txt

值替换

替换test为TEST

[root@centos7 ~]# sed 's/test/TEST/' sedpractice.txt   #只替换一行中匹配到的第一个值

[root@centos7 ~]# sed 's/test/TEST/g' sedpractice.txt   #g 全局替换

删除$

[root@centos7 ~]# sed 's/\$//' sedpractice.txt

去掉行首数字

[root@centos7 ~]# sed  's/^[0-9]*//g' sedpractice.txt 

& 替换,可将值添加到匹配的值之前或之后

例:day 220

将this is 添加到 day前面

[root@centos7 ~]# sed 's/day/ this is &/' sedpractice.txt

>>this is day 220

将this is 添加到 day后面

[root@centos7 ~]# sed 's/day/& this is/' sedpractice.txt

>>day this is 220

文件另存为

将1,2行写入到file.txt中

[root@centos7 ~]# sed '1,2 w file.txt' sedpractice.txt

将匹配到test的值替换为TEST之后,将缓存全部写入到test.txt中

[root@centos7 ~]# sed 's/test/TEST/g' sedpractice.txt >test.txt   #缓存中的都存过去

将匹配到test的值替换为TEST之后,只将替换后的行写入到test.txt中

[root@centos7 ~]# sed 's/test/TEST/ w test.txt' sedpractice.txt   # 只存替换后的行

从文件中读取内容加到匹配的行后面

从la.txt文件中读取内容加到匹配到test的行后面

[root@centos7 ~]# sed '/test/r la.txt' sedpractice.txt

匹配后退出

匹配到test就退出

[root@centos7 ~]# sed '/test/q' sedpractice.txt

 

 

 

 

 

 

posted @ 2020-07-16 20:57  BackingStar  阅读(262)  评论(0编辑  收藏  举报