九、文本处理三剑客——sed

九、文本处理三剑客------sed

9.1 sed介绍

作用:将通过sed命令处理的内容输出到屏幕上或者保存在文件中。

相比于vim文本编辑器来说,sed命令在修改文件的时候具有"非交互式"的特点。

sed工作过程:

  • sed读取一行内容到临时缓冲区中(又称模式空间);
  • 默认情况下,sed会对所有行执行相应指令,如果设置了匹配特定行则只会对匹配到的行执行相应指令;
  • 对当前行执行完指令后,清空模式空间中的内容,读取下一行后再执行相应指令,直到将文件中所有数据处理完毕。

语法:sed [options] [SCRIPT] [INPUTFILE...]

选项:

bash 复制代码
-n  只显示经过sed指令处理的内容,不输出模式空间中的内容;
-i  直接编辑原文件,默认不对原文件进行操作;   
-e  直接在命令行模式上进行sed的动作编辑,多个子命令之间可以用分号隔开,如:  
	sed -e 'command1' -e 'command2'  filename
	sed  'command1;command2...'  filename
	sed {command1;command2} filename
	-e script表示以选项中指定的script来处理输入的文本文件;
-r 表示使用扩展正则表达式                                  

地址定界:

bash 复制代码
不写地址定界  表示对文件所有行进行操作;
num1,num2  对文件的num1-num2行内容进行处理;如果是$表示最后一行,例如:1,3或者4,$;
first~step  例如:1~3,对文件的第1,4,7,10......行内容进行处理;
num1,+N   对文件的第num行以及以后的N行内容进行处理
/pattern/  表示能够被正则表达式匹配到的行,/pattern/I,表示匹配时忽略大小写,例如:/^hello$/I;
0,/pattern/或者1,/pattern/ 从第一行开始到能够被正则表达式匹配到的行;
/pattern/,/pattern/  表示被正则表达式匹配到的行内的所有行;

对匹配到的文件内容的操作:

bash 复制代码
#打印文本到屏幕相关的指令
p:打印匹配的行。
=:打印匹配的行号。
l(L字母的小写):显示行时显示结束符$、制表符\t等。
#替换相关的指令
c:行替换,匹配到的字符串所在行替换成该选项后面的字符串。
a:增加,a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)。如果sed处理的为空文件无法新增内容。
i:插入,i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行)。如果sed处理的为空文件无法新增内容。
s:字符替换,替换指定字符,s/pattern/replacement/flag。
	flag可以指定g(表示全局替换,默认只替换每行第一个)num1(表示对匹配到的第几个内容进行替换),i(不区分大小写),p(如果成功替换则打印)
	& :代表匹配到的内容
y:字符转换,y/abcd/1234/,转换前后的字符长度必须相同。
#删除文本指令
d:删除匹配到的行。
#其它指令
! :对指定行以外的所有行应用命令。
n:读取下一行替换当前模式空间内容。
r:读入文件内容追加到匹配行后面。
w /dir/filename:将匹配到的内容另存到/dir/filename中。

9.2 sed示例

示例1:显示文件内容

bash 复制代码
#示例文件
[root@server ~]# vim  /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
bash 复制代码
#sed指令默认会将模式空间的内容和指令处理的内容均输出
[root@server ~]# sed 1,3p /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2

#-n只显示指令处理的内容
[root@server ~]# sed -n 1,3p /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
[root@server ~]# sed -n '1,3{=;p}' /shell/hosts
1
192.168.168.100 www.jungle.org
2
192.168.168.101 www.rhce.com
3
192.168.168.102 www.rhce.org
[root@server ~]# sed -n  '1=;2=;3=;1p;2p;3p' /shell/hosts
1
192.168.168.100 www.jungle.org
2
192.168.168.101 www.rhce.com
3
192.168.168.102 www.rhce.org
[root@server ~]# sed -n '1,3{l}' /shell/hosts
[root@server ~]# sed -n '1,3{l}' /shell/hosts
192.168.168.100 www.jungle.org$
192.168.168.101 www.rhce.com$
192.168.168.102 www.rhce.org$
[root@server ~]# sed -n /100/p /shell/hosts
192.168.168.100 www.jungle.org
[root@server ~]# sed -n /10/,/com/p /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
#打印除了第一行以外的行内容
[root@server ~]# sed -n  '1 ! p' /shell/hosts
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed -n 'n;=;p' /shell/hosts
2
192.168.168.101 www.rhce.com
4
192.168.168.200 www.test.com
6
192.168.168.92 node2
[root@server ~]# sed -n '=;p;n' /shell/hosts
1
192.168.168.100 www.jungle.org
3
192.168.168.102 www.rhce.org
5
192.168.168.91 node1

[root@server ~]# sed -n  '1 w /shell/hosts1' /shell/hosts
[root@server ~]# cat /shell/hosts1
192.168.168.100 www.jungle.org

示例2:文本替换,不加-i选项不会修改文件内容

bash 复制代码
[root@server ~]# sed  /rhce/c'this is test1' /shell/hosts
192.168.168.100 www.jungle.org
this is test1
this is test1
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed  /rhce/a'this is test1' /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
this is test1
192.168.168.102 www.rhce.org
this is test1
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed  /rhce/i'this is test1' /shell/hosts
192.168.168.100 www.jungle.org
this is test1
192.168.168.101 www.rhce.com
this is test1
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed  's/168/2/' /shell/hosts
192.2.168.100 www.jungle.org
192.2.168.101 www.rhce.com
192.2.168.102 www.rhce.org
192.2.168.200 www.test.com
192.2.168.91 node1
192.2.168.92 node2
[root@server ~]# sed  's/168/2/g' /shell/hosts
192.2.2.100 www.jungle.org
192.2.2.101 www.rhce.com
192.2.2.102 www.rhce.org
192.2.2.200 www.test.com
192.2.2.91 node1
192.2.2.92 node2
[root@server ~]# sed  's/168/2/2' /shell/hosts
192.168.2.100 www.jungle.org
192.168.2.101 www.rhce.com
192.168.2.102 www.rhce.org
192.168.2.200 www.test.com
192.168.2.91 node1
192.168.2.92 node2
#在行首添加注释,行尾加#
[root@server ~]# sed '2 s/^/#/; 3 s/$/#/' /shell/hosts
192.168.168.100 www.jungle.org
#192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org#
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed '2 s/^./#&/;3s/.$/&#/' /shell/hosts
192.168.168.100 www.jungle.org
#192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org#
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
[root@server ~]# sed -n  's/rhce/&test/p' /shell/hosts
192.168.168.101 www.rhcetest.com
192.168.168.102 www.rhcetest.org
[root@server ~]# sed -n   '1,3 s/www/:&:/p' /shell/hosts
192.168.168.100 :www:.jungle.org
192.168.168.101 :www:.rhce.com
192.168.168.102 :www:.rhce.org
[root@server ~]# sed -n '/rhce/ s/www/:&:/p' /shell/hosts
192.168.168.101 :www:.rhce.com
192.168.168.102 :www:.rhce.org
#大小写转换:
	#\u表示将第一个字符小写转大写;
	#\U表示将所有字符小写转大写;
	#\l表示将第一个字符大写转小写;
	#\L表示将所有字符大写转小写
[root@server ~]# echo abc  def | sed 's/abc/\uabc/'
Abc def
[root@server ~]# echo abc  def | sed 's/abc/\Uabc/'
ABC def
[root@server ~]# echo ABC DEF | sed 's/ABC/\lABC/'
aBC DEF
[root@server ~]# echo ABC DEF | sed 's/ABC/\LABC/'
abc DEF
[root@server ~]# sed 's/[a-z]/\u&/g' /shell/hosts
192.168.168.100 WWW.JUNGLE.ORG
192.168.168.101 WWW.RHCE.COM
192.168.168.102 WWW.RHCE.ORG
192.168.168.200 WWW.TEST.COM
192.168.168.91 NODE1
192.168.168.92 NODE2
[root@server ~]# sed 's/[a-z]/\u&/g' /shell/hosts | sed 's/[A-Z]/\l&/g'
192.168.168.100 www.jungle.org
192.168.168.101 www.rhce.com
192.168.168.102 www.rhce.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2
#将每一个单词首字母大写,\b表示单词左侧锚定
[root@server ~]# sed 's/\b[a-z]/\u&/g' /shell/hosts
192.168.168.100 Www.Jungle.Org
192.168.168.101 Www.Rhce.Com
192.168.168.102 Www.Rhce.Org
192.168.168.200 Www.Test.Com
192.168.168.91 Node1
192.168.168.92 Node2

[root@server ~]# sed   'y/rhce/1234/' /shell/hosts
192.168.168.100 www.jungl4.o1g
192.168.168.101 www.1234.3om
192.168.168.102 www.1234.o1g
192.168.168.200 www.t4st.3om
192.168.168.91 nod41
192.168.168.92 nod42
#替换的字符中如果有斜线时,要么使用\将斜线转义,要么换成#或者@分割字符串
[root@server ~]# sed -n '1 s#192#172#p' /shell/hosts1
172.168.168.100 www.jungle.org
[root@server ~]# sed -n '1 s@192@172@p' /shell/hosts1
172.168.168.100 www.jungle.org
[root@server ~]# sed   'y#w#1#' /shell/hosts
192.168.168.100 111.jungle.org
192.168.168.101 111.rhce.com
192.168.168.102 111.rhce.org
192.168.168.200 111.test.com
192.168.168.91 node1
192.168.168.92 node2

示例3:删除文本内容

bash 复制代码
[root@server ~]# sed  '/rhce/d' /shell/hosts
192.168.168.100 www.jungle.org
192.168.168.200 www.test.com
192.168.168.91 node1
192.168.168.92 node2

#巧用替换进行删除
[root@server ~]# sed 's/192//' /shell/hosts
.168.168.100 www.jungle.org
.168.168.101 www.rhce.com
.168.168.102 www.rhce.org
.168.168.200 www.test.com
.168.168.91 node1
.168.168.92 node2
#使用正则匹配进行删除,点代表任意字符,将前两个点括起来表示第一个子表达式\1,将三个点替换为前两个点,则表示删除第三个字符
[root@server ~]# sed  's/\(..\)./\1/' /shell/hosts
19.168.168.100 www.jungle.org
19.168.168.101 www.rhce.com
19.168.168.102 www.rhce.org
19.168.168.200 www.test.com
19.168.168.91 node1
19.168.168.92 node2
[root@server ~]# sed  -r  's/(..)./\1/' /shell/hosts
19.168.168.100 www.jungle.org
19.168.168.101 www.rhce.com
19.168.168.102 www.rhce.org
19.168.168.200 www.test.com
19.168.168.91 node1
19.168.168.92 node2

9.3 案例

示例1:修改selinux的值

bash 复制代码
#修改selinux值为disabled
[root@server ~]# sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

示例2:Linux系统中,有一个文本文件message,需要将文本中的手机号码进行处理,将手机号码转换成XXX-XXXX-XXXX的格式,请你编写Shell程序实现上述功能。

例如文本信息如下:

​ zhangsan 30 18567198188

​ wangwu 20 13390789090

​ lisi 26 15129883716

shell处理后如下:

​ zhangsan 30 185-6719-8188

​ wangwu 20 133-9078-9090

​ lisi 26 151-2988-3716

bash 复制代码
[root@server ~]# vim /shel/message
zhangsan 30 18567198188
wangwu 20 13390789090
lisi 26 15129883716

[root@server ~]# sed -r 's/([1-9]{3})([0-9]{4})([0-9]{4})/\1-\2-\3/' /shel/message
zhangsan 30 185-6719-8188
wangwu 20 133-9078-9090
lisi 26 151-2988-3716

示例3:

bash 复制代码
# 文件内容如下:
    123abc456
	456def123
	567abc789
	789def567

#	要求输出:
	456ABC123
	123DEF456
	789ABC567
	567DEF789
[root@server ~]# vim /shel/test
123abc456
456def123
567abc789
789def567
[root@server ~]# sed  -r 's/([0-9]{3})(.*)([0-9]{3})/\3\U\2\1/' /shell/test
456ABC123
123DEF456
789ABC567
567DEF789

示例4:

bash 复制代码
将镜像名:
registry.aliyuncs.com/google_containers/kube-apiserver:v1.30.4
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.30.4
registry.aliyuncs.com/google_containers/kube-scheduler:v1.30.4
registry.aliyuncs.com/google_containers/kube-proxy:v1.30.4
registry.aliyuncs.com/google_containers/coredns:v1.11.1
registry.aliyuncs.com/google_containers/pause:3.9
registry.aliyuncs.com/google_containers/etcd:3.5.12-0
更名为:
registry.k8s.io/kube-apiserver:v1.30.4
registry.k8s.io/kube-controller-manager:v1.30.4
registry.k8s.io/kube-scheduler:v1.30.4
registry.k8s.io/kube-proxy:v1.30.4
registry.k8s.io/coredns/coredns:v1.11.1
registry.k8s.io/pause:3.9
registry.k8s.io/etcd:3.5.12-0
bash 复制代码
[root@server ~]# vim /shell/images
registry.aliyuncs.com/google_containers/kube-apiserver:v1.30.4
registry.aliyuncs.com/google_containers/kube-controller-manager:v1.30.4
registry.aliyuncs.com/google_containers/kube-scheduler:v1.30.4
registry.aliyuncs.com/google_containers/kube-proxy:v1.30.4
registry.aliyuncs.com/google_containers/coredns:v1.11.1
registry.aliyuncs.com/google_containers/pause:3.9
registry.aliyuncs.com/google_containers/etcd:3.5.12-0
[root@server ~]# sed  -n 's#registry.aliyuncs.com/google_containers#registry.k8s.io#p' /shell/images
registry.k8s.io/kube-apiserver:v1.30.4
registry.k8s.io/kube-controller-manager:v1.30.4
registry.k8s.io/kube-scheduler:v1.30.4
registry.k8s.io/kube-proxy:v1.30.4
registry.k8s.io/coredns:v1.11.1
registry.k8s.io/pause:3.9
registry.k8s.io/etcd:3.5.12-0
相关推荐
落羽的落羽1 小时前
【项目】C++从零实现JsonRpc框架——项目引入
linux·服务器·开发语言·c++·人工智能·算法·机器学习
宇钶宇夕1 小时前
劳易测 AMS3048i(AMS348i)激光测距 博途 TIA 完整详细组态教程
运维·自动化
菜_小_白1 小时前
tcpdump
linux·网络·测试工具·http·tcpdump
折翅嘀皇虫2 小时前
【无标题】steal_work_thread_pool
服务器·前端·算法
zzzsde2 小时前
【Linux】线程概念与控制(3):线程ID&&C++封装线程
linux·运维·服务器·开发语言·算法
消失的旧时光-19432 小时前
C 语言如何实现“面向对象”?—— 从 struct + 函数指针,到 Linux 内核设计思想
linux·c语言·开发语言
不做无法实现的梦~2 小时前
Linux 新手到日常运维操作指南
linux·运维·服务器
xingfujie2 小时前
第3章 安装 kubeadm/kubelet/kubectl
linux·云原生·容器·kubernetes·kubelet
测试员周周2 小时前
【Appium 系列】第09节-数据驱动测试 — YAML 数据 + parametrize
服务器·数据库·人工智能·python·测试工具·语言模型·appium