目录
命令介绍
sed能同时处理多个文件多行的内容
sed是一个很好的文件处理工具,本身是一个管道命令。能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件匹配的数据内容选取之后再输入到屏幕,可以把只匹配到模式的内容输入到屏幕上。还可以对原文件数据内容行进行替换、删除、新增等特定工作改动,但是不会再屏幕上返回结果。
sed命令格式
sed 参数 ‘command’ 源文件(s)
sed 参数 -f scriptfile 源文件(s)
sed 命令参数
- -h或–help:显示帮助;
- -V或–version:显示版本信息;
- -n或–quiet或——silent∶使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来;
- -e或–expression=<script>∶直接在命令行模式上进行sed动作编辑,此为默认选项;
- -f<script文件>或–file=<script文件>∶以选项中指定的script文件中的格式来处理输入的文本文件;
- -r∶sed 的动作支援的是延伸型正规表示法的语法。(预设是基础正规表示法语法)
- -i∶直接修改读取文件的内容,而不在屏幕输出。
sed标记参数(就是前面没有-的参数)也可以称之为编辑器
a∶在定位行后新增内容, a 的后面可以接字符串,而这些字符串会在新的一行出现(当前行的下一行);
c∶用新文本替换定位文本, c 的后面可以接字符串,这些字符串可以取代 n1,n2 之间的行;
d∶删除定位行,因为是删除啊,所以 d 后面通常不接任何咚咚;
g∶表示行内全面替换。 --global
i∶在定位行前插入新内容, i 的后面可以接字符串,而这些字符串会在新的一行出现(当前行的上一行);
I∶还是| 显示与八进制ACSII代码等价的控制符;
n∶从另一个文件中读文本下一行,并从下一条命令而不是第一条命令开始对其的处理;
N∶在数据流中添加下一行以创建用于处理的多行组;
p∶列印,亦即将某个选择的资料印出。通常 p 会与参数 sed -n 一起运作;
q∶第一个模式匹配完成后退出或立即退出;
r∶后面接filename,表示从另一个文件中读文本,类似输入重定向 <
s∶使用指定模式去替换匹配到模式,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表达式!例如 1,20s/old/new/g;
w∶后面接filename,表示把行写入一个文件;
x ∶表示互换模板块中的文本和缓冲区中的文本;
y ∶表示把一个字符翻译为另外的字符(但是不用于正则表达式)
=∶表示打印行号;
{}∶当用到sed不同的标记参数(就是前面没有-的参数)时,用大括号{},且不同标记参数之间用分号隔开。
sed行号使用方法
# 使用行号,可以是一个简单数字,或是一个行号范围
xp x为行号
x,yp 表示行号从x到y
/pattern/p 查询包含模式的行
/pattern/,/pattern/ p 查询包含两个模式的行
/pattern/,xp 将匹配到patern的行到在给定行号的所有内容打印出来
x,/pattern/p 通过行号和模式查询匹配的行
x,y!p 查询不包含指定行号x和y的行
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
实例演练及总结 — sed命令匹配行
准备测试文件0
[root@myhost script]# cat /home/script/log2019.log
2019-01
2019-02
2019-03
2019-04
2019-05
2019-06
2019-07
2019-08
2019-09
2019-10
2019-11
2019-12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
标记参数p的打印功能
#之所以会将文件内容每一行打印两遍是因为sed没有参数时是默认会打印文件中的所有行。
[root@myhost script]# sed 'p' /home/script/log2019.log
2019-01
2019-01
2019-02
2019-02
2019-03
2019-03
2019-04
2019-04
2019-05
2019-05
2019-06
2019-06
2019-07
2019-07
2019-08
2019-08
2019-09
2019-09
2019-10
2019-10
2019-11
2019-11
2019-12
2019-12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
标记参数p打印指定的行
[root@myhost script]# sed '2p' /home/script/log2019.log
2019-01
2019-02 ==》
2019-02 ==》打印文件的第二行,第二行之所以会打印两遍是因为sed默认会打印文件中的所有行。
2019-03
2019-04
2019-05
2019-06
2019-07
2019-08
2019-09
2019-10
2019-11
2019-12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
–n参数和标记参数p一起表示只打印匹配的行。
[root@myhost script]# sed -n 'p' /home/script/log2019.log
[root@myhost script]# cat' /home/script/log2019.log
2019-01
2019-02
2019-03
2019-04
2019-05
2019-06
2019-07
2019-08
2019-09
2019-10
2019-11
2019-12
#标记打印文件中的第二行
[root@myhost script]# sed -n ‘2p’ /home/script/log2019.log
2019-02
#标记打印文件中的第一行至第三行
[root@myhost script]# sed -n ‘1,3p’ /home/script/log2019.log
2019-01
2019-02
2019-03
#标记打印文件中匹配2019-05字符串的行,匹配的内容需要写在’/ /'里面
[root@myhost script]# sed -n ‘/2019-05/p’ /home/script/log2019.log
2019-05
#标记打印文件中从开始匹配到02的行到第4行的所有内容;
#但是如果匹配到02所处的行在第4行之后,则仅打印匹配到02的那一行。
[root@myhost script]# sed -n ‘/02/, 4p’ /home/script/log2019.log
2019-02
2019-03
2019-04
#标记打印文件中从开始匹配到09的行到第4行的所有内容;
#由于09处于文件中的第9行,则只打印出匹配到09的那一行
[root@myhost script]# sed -n ‘/09/, 4p’ /home/script/log2019.log
2019-09
#标记打印文件中从开始匹配03的那一行到匹配09的那一行中间所有的内容,是一个范围
#但是如果匹配到前一个模式所处的行在匹配到后一个模式的行之后,则仅打印匹配到后一个模式的行以及之后的所有内容。
[root@myhost script]# sed -n ‘/03/, /09/p’ /home/script/log2019.log
2019-03
2019-04
2019-05
2019-06
2019-07
2019-08
2019-09
[root@myhost script]# sed -n ‘/09/, /03/p’ /home/script/log2019.log
2019-09
2019-10
2019-11
2019-12
[root@myhost script]# sed -n ‘/08/, /03/p’ /home/script/log2019.log
2019-08
2019-09
2019-10
2019-11
2019-12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
行号的打印
当用到sed不同的标记参数(就是前面没有-的参数)时,可以用大括号{}把所有标记参数括起来,且不同标记参数之间用分号(;)隔开。
#先打印行号,再标记打印行内容
[root@myhost script]# sed -n '{=;p}' /home/script/log2019.log
1
2019-01
2
2019-02
3
2019-03
4
2019-04
5
2019-05
6
2019-06
7
2019-07
8
2019-08
9
2019-09
10
2019-10
11
2019-11
12
2019-12
#打印第二行到第四行的内容并先显示行号
[root@myhost script]# sed -n ‘2,4 {=;p}’ /home/script/log2019.log
2
2019-02
3
2019-03
4
2019-04
#打印文件的第1行到第2行的内容,并先打印行号
[root@myhost script]# sed -n ‘1,2{=;p}’ /home/script/log2019.log
1
2019-01
2
2019-02
#打印文件的非(第1行到第2行)的内容,并先打印行号
[root@myhost script]# sed -n ‘1,2!{=;p}’ /home/script/log2019.log
3
2019-03
4
2019-04
5
2019-05
6
2019-06
7
2019-07
8
2019-08
9
2019-09
10
2019-10
11
2019-11
12
2019-12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
实例演练及总结 — sed命令(扩展)正则表达式
注意:必须结合-r选项使用, 匹配的模式首先都要写在’/ /'中
正则表达式
准备测试文件1
[root@myhost yuki]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
### BRIDGE MODE ###
IPADDR=10.0.0.25
NETMASK=255.255.255.0
DNS=8.8.8.8
GATEWAY=10.0.0.
### NAT MODE ###
#IPADDR=192.168.22.244
#NETMASK=255.255.255.0
#DNS=8.8.8.8
#DNS=192.168.22.1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
标记参数q:第一个模式匹配完成后退出或立即退出
#打印前5行
[root@myhost yuki]# sed '5q' /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
- 1
- 2
- 3
- 4
- 5
- 6
- 7
打印以#开头的行
[root@myhost yuki]# sed -n '/^#/p' /etc/sysconfig/network-scripts/ifcfg-eth0
### BRIDGE MODE ###
### NAT MODE ###
#IPADDR=192.168.22.244
#NETMASK=255.255.255.0
#DNS=8.8.8.8
#DNS=192.168.22.1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
打印以非#开头的行
[root@myhost yuki]# sed -n '/^#/!p' /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.0.25
NETMASK=255.255.255.0
DNS=8.8.8.8
GATEWAY=10.0.0.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
打印以非#开头的行和非空白行^$
[root@myhost yuki]# sed -n '/^#/!{/^$/!p}' /etc/sysconfig/network-scripts/ifcfg-eth0
[root@myhost yuki]# egrep -v '^#|^$' /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=10.0.0.25
NETMASK=255.255.255.0
DNS=8.8.8.8
GATEWAY=10.0.0.1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
准备测试文件2
[root@myhost script]# cat /home/script/mylife.txt
hello world
hello. linux
how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
标记参数{}
{}∶当用到sed不同的标记参数(就是前面没有-的参数)时,用大括号{},且不同标记参数之间用分号隔开。
[root@myhost ~]# sed '/are/{s/are/is/g;s/you/she/g}' /home/script/mylife.txt
#hello world
#hello. linux
#how is she?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
标记参数s + 标记参数g
s:使用指定模式去替换匹配到模式,可以直接进行取代的工作!通常这个 s 的动作可以搭配正规表达式。
g∶表示行内全面替换。 --global
- 和正则表达式^搭配 ( ^在匹配到的模式所在行行首进行操作)
##### 在匹配到world行进行操作 #####
[root@myhost script]# sed '/world/s/^/hi,/g ' /home/script/mylife.txt
hi,hello world
hello. linux
how are you?
i am fine
thanks, and you?
在匹配到hello行进行操作
#把所有匹配到hello的行中出现的第1次出现的字母l替换成w
[root@dscq4 ~]# sed '/hello/s/l/w/ ’ /home/script/mylife.txt
#hewlo world
#hewlo. linux
#how are you?
i am fine
thanks, and you?
#把所有匹配到hello的行中出现的第2次出现的字母l替换成w
[root@dscq4 ~]# sed '/hello/s/l/w/2 ’ /home/script/mylife.txt
#helwo world
#helwo. linux
#how are you?
i am fine
thanks, and you?
#把所有匹配到hello的行中出现的所有字母l替换成w
[root@myhost ~]# sed '/hello/s/l/w/g ’ /home/script/mylife.txt
#hewwo worwd
#hewwo. winux
#how are you?
i am fine
thanks, and you?
在匹配到hello行进行操作
#把所有匹配到hello的行中出现的所有字母l替换成w 并打印2遍
[root@myhost ~]# sed '/hello/s/l/w/2p ’ /home/script/mylife.txt
#helwo world
#helwo world
#helwo. linux
#helwo. linux
#how are you?
i am fine
thanks, and you?
#把所有匹配到hello的行中第2次及第2次之后出现字母l替换成w
[root@myhost ~]# sed '/hello/s/l/w/2g ’ /home/script/mylife.txt
#helwo worwd
#helwo. winux
#how are you?
i am fine
thanks, and you?
[root@dscq4 ~]#
在每行进行操作
[root@myhost ~]# sed ‘s/^/Start /g’ /home/script/mylife.txt
Start hello world
Start hello. linux
Start how are you?
Start i am fine
Start thanks, and you?
#指定的1-3行,在其行首添加#,一般都用在进行注释当中
[root@dscq4 ~]# sed ‘1,3s/^/#/g’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 和正则表达式&搭配
&保存匹配到的模式并用来替换其他字符,如:s/love/22&22/,love这22love22。
[root@myhost script]# sed 's/linux/jie+&/g' /home/script/mylife.txt
hello world
hello. jie+linux
how are you?
i am fine
thanks, and you?
[root@myhost script]# sed ‘s/linux/&+jie/g’ /home/script/mylife.txt
hello world
hello. linux+jie
how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 和正则表达式$搭配( $在匹配到的模式所在行行尾进行操作)
##### 在匹配到are行进行操作 #####
[root@myhost script]# sed '/are/s/$/问号/g' /home/script/mylife.txt
hello world
hello. linux
how are you?问号
i am fine
thanks, and you?
在每行进行操作
[root@myhost ~]# sed ‘s/$/ end/g’ /home/script/mylife.txt
hello world end
hello. linux end
how are you? end
i am fine end
thanks, and you? end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 和正则表达式(模式)搭配,(模式)表示按照此模式将内容进行分组
#在匹配到yo内容所在的行进行操作
#操作:将所有"?" 替换成 "问号"
[root@myhost script]# sed '/you/s/\(?\)/问号/g' /home/script/mylife.txt
hello world
hello. linux
how are you问号
i am fine
thanks, and you问号
#操作:将所有"?" 替换成 “组内容+问号”
[root@myhost script]# sed ‘/you/s/(?)/\1问号/g’ /home/script/mylife.txt
hello world
hello. linux
how are you?问号
i am fine
thanks, and you?问号
[root@myhost script]# sed ‘/you/s/(?)/\1\1\1问号/g’ /home/script/mylife.txt
hello world
hello. linux
how are you???问号
i am fine
thanks, and you???问号
[root@myhost script]# sed ‘/you/s/(.*)/\1你好吗/g’ /home/script/mylife.txt
hello world
hello. linux
how are you?你好吗
i am fine
thanks, and you?你好吗
#.*表示任意字符,\1表示引用第一个分组
#因为是匹配的任意字符,所以整个行的内容都被分为一组了,在添加内容的时候就添加到结尾了
[root@myhost script]# sed ‘/you/s/(.*)/你好吗? \1/g’ /home/script/mylife.txt
hello world
hello. linux
你好吗? how are you?
i am fine
你好吗? thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
标记参数a
a∶在定位行后新增内容, a 的后面可以接字符串,而这些字符串会在新的一行出现(当前行的下一行)
标记参数i
i∶在定位行的前一行插入新内容, i 的后面可以接字符串,而这些字符串会在新的一行出现(当前行的上一行)
[root@myhost script]# sed '/are/i my name is yuki' /home/script/mylife.txt
hello world
hello. linux
my name is yuki 《==
how are you? 《==
i am fine
thanks, and you?
[root@myhost script]# sed ‘/are/i\my name is yuki’ /home/script/mylife.txt
hello world
hello. linux
my name is yuki 《==
how are you?
i am fine
thanks, and you?
[root@myhost script]# sed ‘/are/i/my name is yuki’ /home/script/mylife.txt
hello world
hello. linux
/my name is yuki 《==
how are you?
i am fine
thanks, and you?
插入多行要用\n来换行
[root@myhost ~]# sed ‘/are/i my name is yuki what is your name?’ /home/script/mylife.txt
hello world
hello. linux
my name is yuki what is your name?
how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed ‘/are/i my name is yuki\n what is your name?’ /home/script/mylife.txt
hello world
hello. linux
my name is yuki
what is your name?
how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
命令参数-e
-e或–expression=
##### 删除#开头的行以及匹配到and字符串的行 #####
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed -e '/^#/d' -e '/and/d' /home/script/mylife.txt
i am fine
[root@myhost ~]# sed ‘/are/{s/are/is/g;s/you/she/g}’ /home/script/mylife.txt
#hello world
#hello. linux
#how is she?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
命令参数-i
-i∶直接修改读取文件的内容,而不在屏幕输出。
[root@myhost ~]# cat /home/script/mylife.txt
hello world
hello. linux
how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed -i ‘1,3s/^/#/g’ /home/script/mylife.txt
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
标记参数d
d∶删除定位行,因为是删除啊,所以 d 后面通常不接任何咚咚;
##### 删除#号开始的行 #####
##### 这里没有加-i,相当于对文件进行了操作但是最后没有保存 #####
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed '/^#/d' /home/script/mylife.txt
i am fine
thanks, and you?
删除非#号开始的行
这里没有加-i,相当于对文件进行了操作但是最后没有保存
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed ‘/^#/!d’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
删除文件的第一行,即首行
[root@myhost ~]# sed ‘1d’ /home/script/mylife.txt
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed ‘^d’ /home/script/mylife.txt
sed: -e expression #1, char 1: unknown command: `^’
删除文件的最后一行,即尾行
[root@myhost ~]# sed ‘$d’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
删除文件的1-3行,是范围
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# sed ‘1,3d’ /home/script/mylife.txt
i am fine
thanks, and you?
删除文件匹配到thanks的行
[root@myhost ~]# sed ‘/thanks/d’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
删除文件匹配到包含字母o的行
[root@myhost ~]# sed ‘/o/d’ /home/script/mylife.txt
i am fine
删除文件匹配到单词you的行,<> 作单词牟定#####
[root@myhost ~]# sed ‘/<you>/d’ /home/script/mylife.txt
#hello world
#hello. linux
i am fine
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
sed引用变量
第1种:当sed命令里面没有默认的变量(标记参数)时可以把单引号改成双引号;
第2种:当sed命令里面有默认的变量(标记参数)时,那自定义的变量需要加单引号,且sed里面的匹配语句必须用单引号,以及标记参数也要加$符号
[root@myhost ~]# cat /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
[root@myhost ~]# name=yuki
##把匹配linux的字符替换成变量的值
[root@myhost ~]# sed ‘s/linux/$name/’ /home/script/mylife.txt ==单引号无法解析变量的值
#hello world
#hello. KaTeX parse error: Expected 'EOF', got '#' at position 11: name 《== #̲how are you? i …name/" /home/script/mylife.txt 双引号可以解析变量的值
#hello world
#hello. yuki 《
#how are you?
i am fine
thanks, and you?
#KaTeX parse error: Expected 'EOF', got '#' at position 10: a其实标记参数a #̲#当sed命令也有默认变量时,…a ‘$name’’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
yuki 《==
#KaTeX parse error: Expected 'EOF', got '#' at position 10: i其实标记参数i #̲#当sed命令也有默认变量时,…i ‘$name’’ /home/script/mylife.txt
#hello world
#hello. linux
#how are you?
i am fine
yuki
thanks, and you?
[root@myhost ~]# sed ‘i ‘KaTeX parse error: Expected 'EOF', got '#' at position 38: …life.txt yuki #̲hello world yuk…name’’ /home/script/mylife.txt
#hello world
yuki
#hello. linux
yuki
#how are you?
yuki
i am fine
yuki
thanks, and you?
yuki
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
sed的其它高级使用
1)把正在用sed操作的文件内容追加到另外一个文件中
[root@myhost ~]# sed 's/are/is/w ./test.txt' /home/script/mylife.txt
#hello world
#hello. linux
#how is you?
i am fine
thanks, and you?
[root@myhost ~]# cat ./test.txt
#how is you?
[root@dscq4 ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
2)读取一个文件到正在用sed操作的文件中
#在匹配how的行,读进来另一个文件的内容,读进来的文件的内容会插入到匹配Ethernet的行后
[root@dscq4 ~]# sed '/how/r /home/script/mylife.txt' ./test.txt
#how is you? 《==
#hello world
#hello. linux
#how are you?
i am fine
thanks, and you?
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e9f16cbbc2.css" rel="stylesheet">
</div>