Shell脚本文本三剑客之sed编辑器(拥明月入怀,揽星河入梦)

一、sed编辑器简介

    sed是一种流编辑器,流编辑器会在编辑器处理数据前基于预先提供的一组规则来编辑数据。

sed编辑器可以根据命令来处理数据流中的数据,通过多种转换修改流经它的文本,这些命令要么从命令行中输入,要么存储在一个命令文本文件中。

sed是一个面向字符流的非交互式编辑器,也就是说 sed 不允许用户与它进行交互操作。

二、sed工作流程

1.读取
sed从输入流(文件、管道、标准输入)中读取一行内容并存储到临时的内存缓冲区中(又称模式空间,pattern space)。

2.执行
默认情况下,所有的sed命令都在模式空间中顺序地执行,除非指定了行的地址,否则sed命令将会在所有的行上依次执行。

3.显示
发送修改后的内容到输出流。在发送数据后,模式空间将会被清空。在所有的文件内容都被处理完成之前,上述过程将重复执行,直至所有内容被处理完。

注:默认情况下所有的sed命令都是在模式空间内执行的,不会影响到文件中的内容,除非是用重定向输出到文件或使用-i选项。

三、sed命令

sed [选项] 操作 文件名…

常用选项
在这里插入图片描述
常用操作
在这里插入图片描述

执行多个操作的方法

  • sed -e ‘操作1’ -e ‘操作2’ … 文件名
  • sed -e ‘操作1;操作2;…’ 文件名

四、sed命令的使用

1.sed打印文件内容(p)

(1)打印文件所有行

    sed本身有执行操作后自动打印的效果,-n禁用,p操作也可以打印,并且可以限定条件

sed ’ ’ 文件名
sed -n ‘p’ 文件名

//使用sed自动打印
[root@localhost1 sedtest]#sed '' file1
one
two
three
four
five
six
seven
eight
nine
ten
 
//使用sed的p操作打印
[root@localhost1 sedtest]#sed -n 'p' file1
one
two
three
four
five
six
seven
eight
nine
ten

(2)打印文件指定行

sed -n ‘指定条件p’ 文件名
sed -n ‘/字符串或正则表达式/p’ 文件名

//1.打印指定行(根据行号)
[root@localhost1 sedtest]#sed -n '2p' file1    //打印第2行
two
 
//2.打印指定的多行(根据行号)
[root@localhost1 sedtest]#sed -n '7,$p' file1    //打印从第7行到末尾行
seven
eight
nine
ten
 
//3.打印多行后退出
[root@localhost1 sedtest]#sed '3q' file1    //从头打印3行后退出
one
two
three
 
//4.打印指定行(根据字符串)
[root@localhost1 sedtest]#sed -n '/ve/p' file1    //根据字符串s
five
seven
 
//5.从指定行打印到包含指定字符串的行(根据字符串)    
[root@localhost1 sedtest]#sed -n '7,/nobody/p' /etc/passwd    //打印7到包含nobody的行
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
 
 
//6.打印奇偶行
[root@localhost1 sedtest]#sed -n 'n;p' file1    //n表示跳到下一行,先n实现输出偶数行
two
four
six
eight
ten
[root@localhost1 sedtest]#sed -n 'p;n' file1    //后n实现奇数行
one
three
five
seven
nine
 
//7.正则表达式表示
//根据root开头或包含mail的输出行(包含扩展正则时sed一定需要加-r选项)
[root@localhost1 sedtest]#sed -nr '/^root|mail/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
 
//从第8行打印到包含(r 至少有一个a t)的字符串的行
[root@localhost1 sedtest]#sed -nr '8,/ra{1,}t/p' /etc/passwd
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
 

2.sed增加、插入、替换行(a、i、c)

(1)a操作在指定行下增加行内容

sed ‘指定行范围a 指定内容’ 文件名

//指定第2行下增加行内容
[root@localhost1 ~]#sed '2a ABC' file1
One Two Three
Four Five Six
ABC
Seven Eight Nine
Ten Eleven Twelve
 
//指定2到4行每行下增加行内容
[root@localhost1 ~]#sed '2,4a ABC' file1
One Two Three
Four Five Six
ABC
Seven Eight Nine
ABC
Ten Eleven Twelve
ABC
 

(2)i操作在指定行上插入行内容

sed ‘指定行范围i 指定内容’ 文件名

//指定第2行上插入行内容
[root@localhost1 ~]#sed '2i ABC' file1
One Two Three
ABC
Four Five Six
Seven Eight Nine
Ten Eleven Twelve
 
//指定第1到3行上插入行内容
[root@localhost1 ~]#sed '1,3i ABC' file1
ABC
One Two Three
ABC
Four Five Six
ABC
Seven Eight Nine
Ten Eleven Twelve

(3)c操作指定行替换为一行内容

sed ‘指定行范围c 指定内容’ 文件名

//指定第3行替换为指定行内容
[root@localhost1 ~]#sed '3c ABC' file1
One Two Three
Four Five Six
ABC
Ten Eleven Twelve
 
//指定第3到4行替换为指定行内容(多行变为一行)
[root@localhost1 ~]#sed '3,4c ABC' file1
One Two Three
Four Five Six
ABC
 
//指定包含ve的行替换为指定行内容
[root@localhost1 ~]#sed '/ve/c ABC' file1
One Two Three
ABC
ABC
ABC

3.sed删除文件内容(d)

sed ‘指定条件d’ 文件名
sed ‘/字符串或正则表达式/d’ 文件名

//1.删除所有行(根据行号)
[root@localhost1 sedtest]#sed 'd' file1
 
//2.删除指定行(根据行号)
[root@localhost1 sedtest]#sed '1d' file1    //删除第1行
two
three
four
five
six
seven
eight
nine
ten
 
//3.删除指定的多行(根据行号)
[root@localhost1 sedtest]#sed '2,8d' file1    //删除2到8行
one
nine
ten
 
//4.删除空行
[root@localhost1 sedtest]#sed '/^$/d' /etc/fstab
#
# /etc/fstab
# Created by anaconda on Fri Jul 29 21:42:19 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
 
//5.删除指定的多行(根据字符串)
[root@localhost1 sedtest]#sed '/ve/d' file1    //删除包含ve的行
one
two
three
four
six
eight
nine
ten
 
//6.正则表达式表示
//删除结尾为bash或nologin的行
[root@localhost1 sedtest]#sed -r '/bash$|nologin$/d' /etc/passwd
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
 
//7.从包含第1个字符串的行开始删除,到包含第2个字符串的位置停止删除
//(如果找不到第2个字符串的行,则删到最后一行)
[root@localhost1 sedtest]#sed '/one/,/five/d' file1    //从含one的行删到含five的行
six
seven
eight
nine
ten
 
[root@localhost1 sedtest]#sed '/thr/,/no/d' file1    //从含thr的行开始删,匹配不到含no的行,则删到最后
one
two
 
 
//8.反向删除(删除除了包含指定内容的行)
[root@localhost1 sedtest]#sed '/ve/d' file1    //删除除包含ve的行
five
seven
 

4.sed替换文件内容(s)

sed [选项] ‘行范围 s/旧字符串/新字符串/替换标记’ 文件名

4种替换标记
在这里插入图片描述

//1.不加范围,替换每行查找到的字符串
[root@localhost1 ~]#sed -n 's/root/admin/p' /etc/passwd
admin:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/admin:/sbin/nologin
 
//2.指定范围行,在指定行中替换n次(默认1次)
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/p' /etc/passwd
abc:x:0:0:root:/root:/bin/bash
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/2p' /etc/passwd
root:x:0:0:abc:/root:/bin/bash
 
//3.替换行中所有,使用g
[root@localhost1 ~]#sed -n '/^root/ s/root/abc/gp' /etc/passwd
root:x:0:0:root:/abc:/bin/bash
 
//4.指定行s/^/#/可以实现注释行内容
[root@localhost1 ~]#sed '2 s/^/#/' file1
one two three
#four five six
seven eight nine
ten eleven twelve
 
//5.指定行范围替换
[root@localhost1 ~]#sed '1,3 s/^/#/' file1
#one two three
#four five six
#seven eight nine
ten eleven twelve
 
//指定替换除为空可以实现将指定字符串删除
[root@localhost1 ~]#sed 's/#//' /etc/fstab
 
 /etc/fstab
 Created by anaconda on Fri Jul 29 21:42:19 2022
 
 Accessible filesystems, by reference, are maintained under '/dev/disk'
 See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
 
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0

5.sed使用命令文件执行(-f)

//编写文件包含sed的操作
[root@localhost1 ~]#vim script.txt
/^$/d
/e$/ s/^/#/
1,3 s/#//
 
//测试用文件
[root@localhost1 ~]#vim file2
#one
#two
#three
four
five
 
 
six
seven
eight
 
nine
ten
 
//sed指定文件操作
[root@localhost1 ~]#sed -f script.txt file2
#one
two
#three
four
five
six
seven
eight
#nine
ten

6.sed直接修改文件内容(-i)

//-i直接对文件进行操作
[root@localhost1 ~]#sed -i '/swap/ s/^/#/' /etc/fstab
 
//注释文件中包含swap的行
[root@localhost1 ~]#vim /etc/fstab
# /etc/fstab
# Created by anaconda on Fri Jul 29 21:42:19 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                        xfs     defaults        0 0
UUID=552d20a1-63f1-4209-af23-7bcdb31d1c84 /boot  xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

7.sed扩充操作

(1)替换处&代表需要替换内容处中正则表达式的内容

sed [选项] ‘行范围 s/正则表达式/&/替换标记’ 文件名

//这里的&代表.*swap.*匹配到的内容(即包含swap的行内容),在前面加上#可以实现注释行
[root@localhost1 ~]#sed -n 's/.*swap.*/#&/' /etc/fstab
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

(2)替换处\1…\n\ 对应需要替换内容处每个括号中子表达式(可以是字符串和正则)

sed [选项] ‘s/(表达式1)…(表达式n)/\1\2…\n/’ 文件名

//原文件
[root@localhost1 ~]#cat file3
123456abcdef
 
//字符串子表达示,"\数字" 一一对应
[root@localhost1 ~]#sed 's/(123)(456)(abc)/\3\2\1/' file3
abc456123def
 
//正则子表达示,"\数字" 一一对应
[root@localhost1 ~]#sed -r 's/([0-9]{6})([a-z]{6})/\2\1/' file3
abcdef123456

(3)大小写转换

sed ‘s/[a-z]/\u&/g’ 文件名 (原字符范围前\b实现首字母转大写)
sed ‘s/[A-Z]/\l&/g’ 文件名

//原文件
[root@localhost1 ~]#cat file1
one two three
four five six
seven eight nine
ten eleven twelve
 
//1.全部转为大写
[root@localhost1 ~]#sed 's/[a-z]/\u&/g' file1
ONE TWO THREE
FOUR FIVE SIX
SEVEN EIGHT NINE
TEN ELEVEN TWELVE
 
[root@localhost1 ~]#sed -i 's/[a-z]/\u&/g' file1
 
//2.全部转为小写
[root@localhost1 ~]#sed 's/[A-Z]/\l&/g' file1
one two three
four five six
seven eight nine
ten eleven twelve
 
//3.锚定首字母,实现首字母大小写转换
[root@localhost1 ~]#sed 's/\b[a-z]/\u&/g' file1
One Two Three
Four Five Six
Seven Eight Nine
Ten Eleven Twelve

/U/对应子表达式位置数/E (结合第2条操作实现部分转大小写)

//部分转大小写,实现修改文件前缀名小写转大写
[root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\u\1\E\2/'    //u转开头
Abc.txt
A.txt
B.txt
Efg.txt
Number.txt
Script.txt
 
[root@localhost1 ~]#ls *.txt | sed -r 's/(.+)(\.txt)/\U\1\E\2/'    //U转全部
ABC.txt
A.txt
B.txt
EFG.txt
NUMBER.txt
SCRIPT.txt

(4)将多个文件内容放在一个文件中

sed -i ‘$r 文件1’ 文件2

//往fa、fb文件输入内容
[root@localhost1 ~]#echo 12345678 > fa
[root@localhost1 ~]#echo abcdefgh > fb
 
//合并文件内容到fb文件中
[root@localhost1 ~]#sed -i '$r fa' fb
[root@localhost1 ~]#cat fb
abcdefgh
12345678

(5)复制、剪切粘贴行内容

H 复制 ; G 粘贴

//原文件
[root@localhost1 ~]#cat file2
zhang san
li si
wang wu
zhao liu
qian qi
zhu ba
 
//1.复制粘贴到末尾
[root@localhost1 ~]#sed '1,2H;$G' file2
zhang san
li si
wang wu
zhao liu
qian qi
zhu ba
 
zhang san
li si
 
//2.剪切粘贴到末尾
[root@localhost1 ~]#sed '1,2{H;d};$G' file2
wang wu
zhao liu
qian qi
zhu ba
 
zhang san
li si
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值