参考:http://man.linuxde.net/sed
1.截取指定内容
s为搜索
s/A/B/ 表示将A替换成B,且只替换一次,s/A/B/g 加上g表示全部替换,下面为将搜索到的内容替换成空格。
[root@sesprd tmp]# ifconfig eno16780032 | grep 'inet' | sed 's/^.*inet?//g' inet 10.1.1.179 netmask 255.255.255.0 broadcast 10.1.1.255 inet6 fe80::250:56ff:fe8a:58e3 prefixlen 64 scopeid 0x20<link> [root@sesprd tmp]# ifconfig eno16780032 | grep 'inet[^6]' | sed 's/^.*inet//g' 10.1.1.179 netmask 255.255.255.0 broadcast 10.1.1.255 [root@sesprd tmp]# ifconfig eno16780032 | grep 'inet[^6]' | sed 's/^.*inet//g' | sed 's/netmask.*$//g' 10.1.1.179
注意点。sed -i 会真正改变源文件内容
#源文件内容 [root@sesprd tmp]# nl testsed 1 hello 2 I want to go to work 3 doing working make me happy 4 I want to study linux well [root@sesprd tmp]# ^C #第一行后追加内容 [root@sesprd tmp]# sed '1a my name is gang' testsed hello my name is gang I want to go to work doing working make me happy I want to study linux well #在第一行和第二行之后分别追加内容 root@sesprd tmp]# sed '1,2a come from jiangxi' testsed hello come from jiangxi I want to go to work come from jiangxi doing working make me happy I want to study linux well #删除第2行 [root@sesprd tmp]# sed '2d' testsed | nl 1 hello 2 doing working make me happy 3 I want to study linux well #将第一行替换成hi [root@sesprd tmp]# sed '1c hi' testsed | nl 1 hi 2 I want to go to work 3 doing working make me happy 4 I want to study linux well #第一行和第二行替换成hi [root@sesprd tmp]# sed '1,2c hi' testsed | nl 1 hi 2 doing working make me happy 3 I want to study linux well #在最后一行之前插入let ,之后插入$i改成$a [root@sesprd tmp]# sed -i '$i let' testsed [root@sesprd tmp]# cat -n testsed 1 hello 2 I want to go to work 3 let 4 doing working make me happy
#删除文件中以h或l开头的所有行
[root@sesprd tmp]# sed '/^[hl]/d' testsed
I want to go to work
doing working make me happy
#删除内容为/的行
[root@sesprd tmp]# nl testsed
1 hello
2 /
3 I want to go to work
4 /
5 let
6 /
7 doing working make me happy
8 /
[root@sesprd tmp]# sed -i '///d' testsed
[root@sesprd tmp]# nl testsed
1 hello
2 I want to go to work
3 let
4 doing working make me happy
[root@sesprd tmp]#
#删除空行
[root@sesprd tmp]# nl testsed
1 hello
2 I want to go to work
3 let
4 doing working make me happy
[root@sesprd tmp]#
#删除空行
[root@sesprd tmp]# sed -i '/^$/d' testsed
[root@sesprd tmp]# nl testsed
1 hello
2 I want to go to work
3 let
4 doing working make me happy
#最后一行添加空行
[root@sesprd tmp]# sed '$G' testsed
hello
I want to go to work
let
doing working make me happy
[root@sesprd tmp]# nl testsed
#最后一行添加内容
[root@sesprd tmp]# sed '$a last' testsed
hello
I want to go to work
let
doing working make me happy
last
#删除空格
[root@sesprd tmp]# sed 's/[[:space:]]*//g' testsed
hello
Iwanttogotowork
let
doingworkingmakemehappy
#将第一行字符之间的空格移除
[root@sesprd tmp]# sed -i '1s/[[:space:]]//g' testsed
[root@sesprd tmp]# nl testsed
1 hello
2 I want to go to work
3 let
4 doing working make me happy
#-n 仅显示处理后的结果 获取hello的下一行
[root@sesprd tmp]# sed -n '/^hell/{n;p}' testsed
I want to go to work
[root@sesprd tmp]#
#如果hello被匹配,则移动到匹配行的下一行,替换这一行的want,变为wt,并打印该行,然后继续:
[root@sesprd tmp]# sed '/hello/{ n; s/want/wt/; }' testsed
hello
I wt to go to work
let
doing working make me happy
[root@sesprd tmp]#
#将testsed文件中所有包含hel的行内容写入tt档案
[root@sesprd tmp]# sed '/hel/w tt' testsed
hello
I want to go to work
let
doing working make me happy
[root@sesprd tmp]# nl tt
1 hello
[root@sesprd tmp]#
#testsed里的内容被读进来,显示在与hel匹配的行后面,如果匹配多行,则tt的内容将显示在所有匹配行的下面
[root@sesprd tmp]# sed '/hel/r tt' testsed
hello
hello
I want to go to work
let
doing working make me happy
[root@sesprd tmp]#
-e选项允许在同一行里执行多条命令:
eg: sed -e '1,5d' -e 's/test/check/' file
#打印从第1行开始到第一个包含以le开始的行之间的所有行:
[root@sesprd tmp]# sed -n '1,/^le/p' testsed
hello
I want to go to work
let
显示文件X行到Y行的内容:
#sed -n 'x,yp' filename