15 Shell Script sed命令

发布于:2024-10-09 ⋅ 阅读:(6) ⋅ 点赞:(0)

sed命令

一、sed命令介绍

​ stream editor for filtering and transforming text

​ 非交互式的文本流编辑器,能处理多个文本,支持正则表达式

​ 基本语法格式:

​ sed[option] “AddressCommand” [input-file]

​ sed的option

​ n:sed默认输出一遍处理的文本,-n选项会禁止这一行为

​ e:sed默认接收一个编辑语句,如果由多条可以用-e标识

​ f:sed读取-f指定的文件,解释执行文件内的编辑语句

​ i:sed将修改结果直接覆盖回文件

​ r:sed开启对扩展正则表达式的支持

​ sed的Address

​ sed默认对输入文本的每一行进行处理,可以使用Address来定义需要编辑的范围

​ Address不是必须的,如果为空默认处理每一行

​ 支持正则表达式

​ Address形式:[str[,str]]

​ /str/ 或n(行号)

​ /str/,n 或n,/str/ 或/str/,/str/

​ sed的Command(基础部分)

​ p:打印匹配的行的内容

​ =:打印行号

​ a:指定或匹配的行下方新开一行加入文本信息

​ i:指定或匹配的行上方新开一行加入文本信息

​ d:删除指定或匹配的行

​ s:查找并替换命令

​ q:指定或匹配的行后退出sed命令

二、sed基础使用

[root@localhost ~]# cat test01.txt                                                                     
1 hadoop 123
2 hive 234
3 hbase 456
4 spark 567

​ sed的”增删改查”中的查

# 查第三行的内容
[root@localhost ~]# sed -n '3p' test01.txt
3 hbase 456

# 查看包含234这行的内容
[root@localhost ~]# sed -n '/234/p' test01.txt
2 hive 234

# 查看包含234的行到包含567这行的内容
[root@localhost ~]# sed -n '/234/,/567/p' test01.txt
2 hive 234
3 hbase 456
4 spark 567

# 查看不包含234的行到567这些行的内容
[root@localhost ~]# sed -n '/234/,/456/!p' test01.txt
1 hadoop 123
4 spark 567

​ sed的”增删改查”中的增

# 在第三行下插入一行数据
[root@localhost ~]# sed '3a\hello' test01.txt   
1 hadoop 123
2 hive 234
3 hbase 456
hello
4 spark 567

# 每一行都插入
[root@localhost ~]# sed 'a\hello' test01.txt 
1 hadoop 123
hello
2 hive 234
hello
3 hbase 456
hello
4 spark 567
hello

​ sed的”增删改查”中的删

# 删除第三行
[root@localhost ~]# sed '3d' test01.txt
1 hadoop 123
2 hive 234
4 spark 567

# 包含h的不删
[root@localhost ~]# sed '/h/! d' test01.txt   
1 hadoop 123
2 hive 234
3 hbase 456

​ sed的”增删改查”中的改(查找并替换)

# strom替换hive
[root@localhost ~]# sed 's/hive/strom/' test01.txt
1 hadoop 123
2 strom 234
3 hbase 456
4 spark 567

# 含有h的行,将6替换为9
[root@localhost ~]# sed '/h/s/6/9/' test01.txt
1 hadoop 123
2 hive 234
3 hbase 459
4 spark 567

# 将/etc/inittab最后一行的启动模式改为5
sed -r "s/(id:)[0-6](:initdefault:)/\15\2/" /etc/inittab

三、sed扩展使用

​ sed的Command(扩展)

​ h:缓存指定或匹配的行

​ x:取出缓存的内容替换指定或匹配的行

​ G:取出缓存的内容追加到指定或匹配的行下方

​ n:指定或匹配的行下一行执行下一个操作指令

​ {}:组合Command,用;符号隔开

# 删除第一行和第三行
[root@localhost ~]# sed -e '1d' -e '3d' test01.txt
2 hive 234
4 spark 567

# 显示含有h,不含有34的行
[root@localhost ~]# sed -n '/h/ {/34/! p} ' test01.txt
1 hadoop 123
3 hbase 456

# 将含有hive行的下一行删除
[root@localhost ~]# sed '/hive/{n;d}' test01.txt   
1 hadoop 123
2 hive 234
4 spark 567

# 显示奇数行
[root@localhost ~]# sed '{n;d}' test01.txt      
1 hadoop 123
3 hbase 456

# 显示偶数行
[root@localhost ~]# sed -n '{p;n}' test01.txt
1 hadoop 123
3 hbase 456

# 将第三行复制到第四行后面
[root@localhost ~]# sed -e '3h' -e '4G' test01.txt 
1 hadoop 123
2 hive 234
3 hbase 456
4 spark 567
3 hbase 456

# 将第1行覆盖第3行
[root@localhost ~]# sed -e '1h' -e '3x' test01.txt
1 hadoop 123
2 hive 234
1 hadoop 123
4 spark 567

# 将1替换为a,2换b,3换c
[root@localhost ~]# sed 'y/123/abc/' test01.txt
a hadoop abc
b hive bc4
c hbase 456
4 spark 567

# 删除并保存(永久删除)
[root@localhost ~]# sed -i '2d' test01.txt        
[root@localhost ~]# cat test01.txt
1 hadoop 123
3 hbase 456
4 spark 567