liunx系统sed命令使用

sed '$行数i 新增内容' 文件名在文件里某行上面新增内容

sed '$行数a 新增内容' 文件名在文件里某行下面新增内容

**-i.bak**对源文件进行修改并备份修改之前源文件

#在文件里第5行上面插入cccccc内容
[root@VM-Project_Test /project]# sed '5i cccccc' note_txt 
1
2
3
4
cccccc
5
6
7
8
9
10
#在文件里第5行下面插入cccccc内容
[root@VM-Project_Test /project]# sed '5a cccccc' note_txt 
1
2
3
4
5
cccccc
6
7
8
9
10
#在文件里2行到5行下面插入cccccc内容
[root@VM-Project_Test /project]# sed '2,5a cccccc' note_txt 
1
2
cccccc
3
cccccc
4
cccccc
5
cccccc
6
7
8
9
10

sed 'd' 文件名指定删除文件里某行内容 **-i.bak**对源文件进行修改并备份修改之前源文件

#删除文件里全部内容
[root@VM-Project_Test /project]# sed 'd' note_txt 
#删除文件里第一行内容
[root@VM-Project_Test /project]# sed '1d' note_txt 
2
3
4
5
6
7
8
9
10
#删除文件里第3行到7行内容
[root@VM-Project_Test /project]# sed '3,7d' note_txt 
1
2
8
9
10
#删除文件里第二行和第8行内容
[root@VM-Project_Test /project]# sed '2d;8d' note_txt 
1
3
4
5
6
7
9
10
#删除文本里最后一行内容
[root@VM-Project_Test /project]# sed '$d' note_txt 
1
2
3
4
5
6
7
8
9

sed '内容行数s#需替换的内容#替换的新内容#' 文件名替换文件中内容 **-i**对源文件进行修改

sed -i.bak'内容行数s#需替换的内容#替换的新内容#' 文件名对源文件进行修改自动备份修改之前内容

bash 复制代码
[root@VM-Project_Test /project]# cat note_txt 
1
2
3
4
hjdkfds5nmxcvlm
6
7
8
9
10
#在文件里第5行行首插入11111内容
[root@VM-Project_Test /project]# sed '5s#^#11111#' note_txt 
1
2
3
4
11111hjdkfds5nmxcvlm
6
#在文件里第5行行尾插入22222内容
[root@VM-Project_Test /project]# sed '5s#$#22222#' note_txt 
1
2
3
4
hjdkfds5nmxcvlm22222
6
#在文件里5行到7行行尾插入22222
[root@VM-Project_Test /project]# sed '5,7s#$#22222#' note_txt 
1
2
3
4
hjdkfds5nmxcvlm22222
622222
722222
8
#对文件里第5行里的ds5n替换成0000
[root@VM-Project_Test /project]# cat note_txt 
1
2
3
4
hjdkfds5nmxcvlm
6
[root@VM-Project_Test /project]# sed '5s#ds5n#0000#' note_txt 
1
2
3
4
hjdkf0000mxcvlm
6

sed -n '文本行数p' 文件名读取文件里内容 **-n**默认输出内容到当前屏幕

bash 复制代码
#读取文件里第三行内容
[root@VM-Project_Test /project]# sed -n '3p' note_txt
3
#读取文件里第三行和第七行内容
[root@VM-Project_Test /project]# sed -n '3p;7p' note_txt
3
7
#读取文件里第三行到第七行内容
[root@VM-Project_Test /project]# sed -n '3,7p' note_txt
3
4
5
6
7
#读取文件里最后一行内容
[root@VM-Project_Test /project]# sed -n '$p' note_txt
10
#读取文件里第三行到最后一行内容
[root@VM-Project_Test /project]# sed -n '7,$p' note_txt
7
8
9
10

扩展正则反向引用

扩展正则 sed -r

把所有我需要的内容对两边进行过滤控制,把所需要的内容控制在(.*)里进行反向引用

一个(.*)就是一个分组里的内容\1就是取几个分组里的内容\0之前全部内容

bash 复制代码
#sed反向引用取主机IP
[root@VM-Project_Test /project]# cat test 
	flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
ip add  inet 192.168.100.102  netmask 255.255.252.0  broadcast 10.0.19.255
        inet6 fe80::5054:ff:fe2e:261c  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:2e:26:1c  txqueuelen 1000  (Ethernet)
        RX packets 156078  bytes 105983697 (101.0 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 93268  bytes 14552098 (13.8 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
[root@VM-Project_Test /project]# cat test|sed -n '2p' | sed -r 's#(.*).*inet(.*)netmask(.*)#\1#'
ip add  
[root@VM-Project_Test /project]# cat test|sed -n '2p' | sed -r 's#(.*).*inet(.*)netmask(.*)#\2#'
 192.168.100.102  
[root@VM-Project_Test /project]# cat test|sed -n '2p' | sed -r 's#(.*).*inet(.*)netmask(.*)#\3#'
 255.255.252.0  broadcast 10.0.19.255
[root@VM-Project_Test /project]# cat test|sed -n '2p' | sed -r 's#(.*).*inet(.*)netmask(.*)#\3#'

举例:

某目录下有多个图片文件名字需要进行修改把所有图片文件名字中的finished给去掉修改位如stu_10086_3.jpg

bash 复制代码
[root@VM-Project_Test /project]# ls
stu_10086_1_finished.jpg  stu_10086_3_finished.jpg  stu_10086_5_finished.jpg  stu_10086_7_finished.jpg
stu_10086_2_finished.jpg  stu_10086_4_finished.jpg  stu_10086_6_finished.jpg  stu_10086_8_finished.jpg

#先模糊查找出所需要修改的文件
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"
/project/stu_10086_8_finished.jpg
/project/stu_10086_6_finished.jpg
/project/stu_10086_3_finished.jpg
/project/stu_10086_2_finished.jpg
/project/stu_10086_1_finished.jpg
/project/stu_10086_7_finished.jpg
/project/stu_10086_4_finished.jpg
/project/stu_10086_5_finished.jpg
#sed替换反向引用过滤单取第一个组里的内容
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#\1#'
/project/stu_10086_8
/project/stu_10086_6
/project/stu_10086_3
/project/stu_10086_2
/project/stu_10086_1
/project/stu_10086_7
/project/stu_10086_4
/project/stu_10086_5
#sed替换反向引用过滤单取第二个组里的内容
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#\2#'
.jpg
.jpg
.jpg
.jpg
.jpg
.jpg
.jpg
.jpg
#sed替换反向引用过滤取第一个和二个组里的内容
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#\1\2#'
/project/stu_10086_8.jpg
/project/stu_10086_6.jpg
/project/stu_10086_3.jpg
/project/stu_10086_2.jpg
/project/stu_10086_1.jpg
/project/stu_10086_7.jpg
/project/stu_10086_4.jpg
/project/stu_10086_5.jpg

#\0取所有
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#\0 \1\2#'
/project/stu_10086_8_finished.jpg /project/stu_10086_8.jpg
/project/stu_10086_6_finished.jpg /project/stu_10086_6.jpg
/project/stu_10086_3_finished.jpg /project/stu_10086_3.jpg
/project/stu_10086_2_finished.jpg /project/stu_10086_2.jpg
/project/stu_10086_1_finished.jpg /project/stu_10086_1.jpg
/project/stu_10086_7_finished.jpg /project/stu_10086_7.jpg
/project/stu_10086_4_finished.jpg /project/stu_10086_4.jpg
/project/stu_10086_5_finished.jpg /project/stu_10086_5.jpg
#sed替换反向引用过滤取组里的内容和所有内容进行移动改名
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#mv \0 \1\2#'
mv /project/stu_10086_8_finished.jpg /project/stu_10086_8.jpg
mv /project/stu_10086_6_finished.jpg /project/stu_10086_6.jpg
mv /project/stu_10086_3_finished.jpg /project/stu_10086_3.jpg
mv /project/stu_10086_2_finished.jpg /project/stu_10086_2.jpg
mv /project/stu_10086_1_finished.jpg /project/stu_10086_1.jpg
mv /project/stu_10086_7_finished.jpg /project/stu_10086_7.jpg
mv /project/stu_10086_4_finished.jpg /project/stu_10086_4.jpg
mv /project/stu_10086_5_finished.jpg /project/stu_10086_5.jpg
[root@VM-Project_Test /project]# find /project -name "*_finished.jpg"|sed -r 's#(.*)_finished(.*)#mv \0 \1\2#'|bash
[root@VM-Project_Test /project]# ls
stu_10086_1.jpg  stu_10086_3.jpg  stu_10086_5.jpg  stu_10086_7.jpg
stu_10086_2.jpg  stu_10086_4.jpg  stu_10086_6.jpg  stu_10086_8.jpg