https://www.cnblogs.com/liwei0526vip/p/5644163.html
https://www.cnblogs.com/ginvip/p/6376049.html
https://wenku.baidu.com/view/f836d988d0d233d4b14e69a7.html
1 sed修改ip地址
sed -i \'/^ONBOOT=/c\\ONBOOT=yes\' /etc/sysconfig/network-scripts/ifcfg-en33
sed -i \'/^BOOTPROTO=/c\\BOOTPROTO=static\' /etc/sysconfig/network-scripts/ifcfg-en33
echo \'IPADDR=10.10.10.129\' >> /etc/sysconfig/network-scripts/ifcfg-en33
echo \'PREFIX=24\' >> /etc/sysconfig/network-scripts/ifcfg-en33
echo \'GATEWAY=10.10.10.2\' >> /etc/sysconfig/network-scripts/ifcfg-en33
echo \'DNS1=10.10.10.2\' >> /etc/sysconfig/network-scripts/ifcfg-en33
2、sed语法和常用选项
1、语法
sed [选项] ‘地址 命令’ 文件名称
选项部分,常见选项包括-n,-e,-i,-f,-r选项。
2、常用选项
选项-n
sed默认会把模式空间处理完毕后的内容输出到标准输出,也就是输出到屏幕上,加上-n选项后被设定为安静模式,也就是不会输出默认打印信息,除非子命令中特别指定打印选项,则只会把匹配修改的行进行打印。
例子1:
echo -e \'hello world\\nnihao\' | sed \'s/hello/A/\'
结果:
A world
nihao
例子2:
echo -e \'hello world\\nnihao\' | sed -n \'s/hello/A/\'
结果:加-n选项后什么也没有显示。
例子3:
echo -e \'hello world\\nnihao\' | sed -n \'s/hello/A/p\'
结果:A world/
说明:-n选项后,再加p标记,只会把匹配并修改的内容打印了出来。
选项-e
如果需要用sed对文本内容进行多种操作,则需要执行多条子命令来进行操作。
例子1:
echo -e \'hello world\' | sed -e \'s/hello/A/\' -e \'s/world/B/\'
结果:A B
例子2:
echo -e \'hello world\' | sed \'s/hello/A/;s/world/B/\'
结果:A B
说明:例子1和例子2的写法的作用完全等同,可以根据喜好来选择,如果需要的子命令操作比较多的时候,无论是选择-e选项方式,还是选择分号的方式,都会使命令显得臃肿不堪,此时使用-f选项来指定脚本文件来执行各种操作会比较清晰明了。
选项-i
sed默认会把输入行读取到模式空间,简单理解就是一个内存缓冲区,sed子命令处理的内容是模式空间中的内容,而非直接处理文件内容。因此在sed修改模式空间内容之后,并非直接写入修改输入文件,而是打印输出到标准输出。如果需要修改输入文件,那么就可以指定-i选项。
例子1:
cat file.txt
hello world
[root@localhost]# sed \'s/hello/A/\' file.txt
A world
[root@localhost]# cat file.txt
hello world
例子2:
[root@localhost]# sed -i \'s/hello/A/\' file.txt
[root@localhost]# cat file.txt
A world
例子3:
[root@localhost]# sed –i.bak \'s/hello/A/\' file.txt
说明:最后一个例子会把修改内容保存到file.txt,同时会以file.txt.bak文件备份原来未修改文件内容,以确保原始文件内容安全性,防止错误操作而无法恢复原来内容。
选项-f
还记得 -e 选项可以来执行多个子命令操作,用分号分隔多个命令操作也是可以的,如果命令操作比较多的时候就会比较麻烦,这时候把多个子命令操作写入脚本文件,然后使用 -f 选项来指定该脚本。
例子1:
echo \"hello world\" | sed -f sed.script
结果:A B
sed.script脚本内容:
s/hello/A/
s/world/B/
说明:在脚本文件中的子命令串就不需要输入单引号了。
选项-r
sed命令的匹配模式支持正则表达式的,默认只能支持基本正则表达式,如果需要支持扩展正则表达式,那么需要添加-r选项。
例子1:
echo \"hello world\" | sed -r \'s/(hello)|(world)/A/g\'
A A
3、命令
sed 操作命令告诉 sed 如何处理由地址指定的各输入行。如果没有指定地址, sed 就会处理输入的所有的行。表 3.sed 命令
|
命 令 |
说 明 |
|
a\\ |
在当前行后添加一行或多行 |
|
c\\ |
用新文本修改(替换)当前行中的文本 |
|
d |
删除行 |
|
i\\ |
在当前行之前插入文本 |
|
h |
把模式空间里的内容复制到暂存缓存区 |
|
H |
把模式空间里的内容追加到暂存缓存区 |
|
g |
取出暂存缓冲区里的内容,将其复制到模式空间,覆盖该处原有内容 |
|
G |
取出暂存缓冲区里的内容,将其复制到模式空间,追加在原有内容后面 |
|
l |
列出非打印字符 |
|
p |
打印行 |
|
n |
读入下一输入行,并从下一条命令而不是第一条命令开始处理 |
|
q |
结束或退出 sed |
|
r |
从文件中读取输入行 |
|
! |
对所选行意外的所有行应用命令 |
|
s |
用一个字符串替换另一个 |
表 4.替换标志
|
g |
在行内进行全局替换 |
|
p |
打印行 |
|
w |
将行写入文件 |
|
x |
交换暂存缓冲区与模式空间的内容 |
|
y |
将字符转换为另一字符(不能对正则表达式使用 y 命令) |
报错信息和退出信息
遇到语法错误时, sed 会向标准错误输出发送一条相当简单的报错信息。但是,如果 sed判断不出错在何处,它会“断章取义”,给出令人迷惑的报错信息。如果没有语法错误, sed将会返回给 shell 一个退出状态,状态为 0 代表成功,为非 0 整数代表失败。
sed使用实例
下面给出测试文件作为输入文件:
|
1 2 3 4 5 6 7 8 9 10 |
[root@Gin scripts]# cat ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
打印: p 命令
命令 p 是打印命令,用于显示模式缓存区的内容。默认情况下, sed 把输入行打印在屏幕上,选项-n 用于取消默认打印操纵。当选项-n 和命令 p 同时出现时, sed 可打印选定的内容
案例1:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@Gin scripts]# sed \'/north/p\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:默认情况下, sed 把所有输入行都打印在标准输出上。如果在某一行匹配到 north, sed就把该行另外打印一遍。
案例2:
|
1 2 3 4 |
[root@Gin scripts]# sed -n \'/north/p\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 |
说明:默认情况下, sed 打印当前缓存区中的输入行。命令 p 指示 sed 将再次打印该行。选项-n 取消 sed 取消默认打印操作。选线-n 和命令配合使用,模式缓冲区内的输入行,只被打印一次。如果不指定-n 选项, sed 就会像上例中那样,打印出重复的行。如果指定了-n,则sed 只打印包含模式 north 的行。
删除: d 命令
命令 d 用于删除输入行。sed 先将输入行从文件复制到模式缓存区,然后对该行执行 sed命令,最后将模式缓存区的内容显示在屏幕上。如果发出的是命令 d,当前模式缓存区的输入行会被删除,不被显示。
案例 3:
|
1 2 3 4 5 6 7 8 9 |
[root@Gin scripts]# sed \'3d\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:删除第 3 行。默认情况下,其余的行都被打印到屏幕上。
案例 4:
|
1 2 3 |
[root@Gin scripts]# sed \'3,$d\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 |
说明:删除从第三行到最后一行内容,剩余各行被打印。地址范围是开始第 3 行,结束最后一行。
案例 5:
|
1 2 3 4 5 6 7 |
[root@Gin scripts]# sed \'/north/d\' ceshi.txt western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 central CT Ann Stephens 5.7 .94 5 13 |
说明:所有包含模式 north 的行都被动删除,其余行被打印。
替换: s 命令
命令 s 是替换命令。替换和取代文件中的文本可以通过 sed 中的 s 来实现, s 后包含在斜杠中的文本是正则表达式,后面跟着的是需要替换的文本。可以通过 g 标志对行进行全局替换
案例 6:
|
1 2 3 4 5 6 7 8 9 10 |
[root@Gin scripts]# sed \'s/west/north/g\' ceshi.txt northnorth NW Charles Main 3.0 .98 3 34 northern WE Sharon Gray 5.3 .97 5 23 southnorth SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:s 命令用于替换。命令末端的 g 表示在行内全局替换;也就是说如果每一行里出现多个west,所有的 west 都会被替换为 north。如果没有 g 命令,则只将每一行的第一 west 替换为 north。
案例 7:
|
1 2 |
[root@Gin scripts]# sed -n \'s/^west/north/p\' ceshi.txt northern WE Sharon Gray 5.3 .97 5 23 |
说明:s 命令用于替换。选线-n 与命令行末尾的标志 p 结合,告诉 sed 只打印发生替换的那些行;也就是说,如果只有在行首找到 west 并替换成 north 时才会打印此行。
案例 8:
|
1 2 3 4 5 6 7 8 9 10 |
[root@Gin scripts]# sed \'s/[0-9][0-9]$/&.5/\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34.5 western WE Sharon Gray 5.3 .97 5 23.5 southwest SW Lewis Dalsass 2.7 .8 2 18.5 southern SO Suan Chin 5.1 .95 4 15.5 southeast SE Patricia Hemenway 4.0 .7 4 17.5 eastern EA TB Savage 4.4 .84 5 20.5 northeast NE AM Main Jr. 5.1 .94 3 13.5 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13.5 |
说明:当“与”符号( &)用在替换串中时,它代表在查找串中匹配到的内容时。这个示例中所有以 2 位数结尾的行后面都被加上.5。
案例 9:
|
1 2 |
[root@Gin scripts]# sed -n \'s/Hemenway/Jones/gp\' ceshi.txt southeast SE Patricia Jones 4.0 .7 4 17 |
说明:文件中出现的所有的 Hemenway 都被替换为 Jones,只有发生变化的行才会打印出来。选项-n 与命令 p 的组合取消了默认的输出。标志 g 的含义是表示在行内全局替换。
案例 10:
|
1 2 3 4 5 6 7 8 9 10 11 |
[root@Gin scripts]# sed \'s/\\(Mar\\)got/\\1linanne/p\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Marlinanne Weber 4.5 .89 5 9 north NO Marlinanne Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:包含在圆括号里的模式 Mar 作为标签 1 保存在特定的寄存器中。替换串可以通过\\1 来引用它。则 Margot 被替换为 Marlinane。
案例 11:
|
1 2 3 4 5 6 7 8 9 10 |
[root@Gin scripts]# sed \'s#3#88#g\' ceshi.txt northwest NW Charles Main 88.0 .98 88 884 western WE Sharon Gray 5.88 .97 5 288 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 88 188 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 188 |
说明:紧跟在 s 命令后的字符就是查找串和替换串之间的分隔符。分隔符默认默认为正斜杠,但可以改变。无论什么字符(换行符,反斜线除外),只要紧跟在 s 命令,就成了新的串分隔符。这个方法在查找包含正斜杠模式时很管用,例如查找路径名或生日。
指定行的范围:逗号
行的范围从文件中的一个地址开始,在另一个地址结束。地址范围可以是行号(例如5,10),正则表达式(例如/Dick/和/Joe/),或者两者的结合(例如/north/,$)范围是闭合的——包含开始条件的行,结束条件的行,以及两者之间的行。如果结束条件无法满足,就会一直操作到文件结尾。如果结束条件满足,则继续查找满足开始条件的位置,范围重新开始。
案例 12:
|
1 2 3 4 5 6 |
[root@Gin scripts]# sed -n \'/west/,/east/p\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 |
说明:打印模式 west 和 east 之间所有的行。如果 west 出现在 east 之后的某一行,则打印的范围从 west 所在行开始,到下一个出现 east 的行或文件的末尾(如果前者未出现)。图中用箭头表示出了该范围。
案例 13:
|
1 2 3 4 |
[root@Gin scripts]# sed -n \'5,/northeast/p\' ceshi.txt southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 |
说明:打印从第 5 行开始第一个以 northeast 开头的行之间的所有行。
案例 14:
|
1 2 3 4 5 6 7 8 9 10 |
[root@Gin scripts]# sed \'/west/,/east/s/$/**VACA**/\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34**VACA** western WE Sharon Gray 5.3 .97 5 23**VACA** southwest SW Lewis Dalsass 2.7 .8 2 18**VACA** southern SO Suan Chin 5.1 .95 4 15**VACA** southeast SE Patricia Hemenway 4.0 .7 4 17**VACA** eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:修改从模式 wast 和 east 之间的所有行,将各行的行尾($)替换为字符串**VACA**。换行符被移到新的字符串后面。
多重编辑: e 命令
-e 命令是编辑命令,用于 sed 执行多个编辑任务的情况下。在下一行开始编辑前,所有的编辑动作将应用到模式缓存区的行上。
案例 15:
|
1 2 3 4 5 6 7 |
[root@Gin scripts]# sed -e \'1,3d\' -e \'s/Hemenway/Jones/\' ceshi.txt southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Jones 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 north NO Margot Weber 4.5 .89 5 9 central CT Ann Stephens 5.7 .94 5 13 |
说明:选项-e 用于进行多重编辑。第一重编辑编辑删除第 1~3 行。第二重编辑将Hemenway 替换为 Jones。因为是逐行进行这两行编辑(即这两个命令都在模式空间的当前行上执行),所以编辑命令的顺序会影响结果。例如,如果两条命令都执行的是替换,前一次替换会影响后一次替换。
追加: a 命令
a 命令是追加命令,追加将新文本到文件中当前行(即读入模式的缓存区行)的后面。不管是在命令行中,还是在 sed 脚本中, a 命令总是在反斜杠的后面。
案例 16:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@Gin scripts]# sed -i \'/^north/a Hello world!\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 Hello world! western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7 .8 2 18 southern SO Suan Chin 5.1 .95 4 15 southeast SE Patricia Hemenway 4.0 .7 4 17 eastern EA TB Savage 4.4 .84 5 20 northeast NE AM Main Jr. 5.1 .94 3 13 Hello world! north NO Margot Weber 4.5 .89 5 9 Hello world! central CT Ann Stephens 5.7 .94 5 13 |
说明:命令 a 用于追加。字符串 Hello, World!被加在以 north 开头的各行之后。如果要追加的内容超过一行,则除最后一行外,其他各行都必须以反斜杠结尾。
插入: i 命令
i 命令是插入命令,类似于 a 命令,但不是在当前行后增加文本,而是在当前行前面插入新的文本,即刚读入缓存区模式的行。
案例 17:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@Gin scripts]# sed -i \'/eastern/i Hello,world!\\ > -----------------------\' ceshi.txt northwest NW Charles Main 3.0 .98 3 34 western WE Sharon Gray 5.3 .97 5 23 southwest SW Lewis Dalsass 2.7
本文仅代表作者观点,不代表百度立场。 |



