linux系统中实现对行的批量替换

1、测试数据

[root@PC3 test]# cat a.txt
e r e y e u e
e e g e 3 h r
1 3 e g e y e
e s e e e e e
e t s t e s r
d g e s w t e
[root@PC3 test]# cat c.txt
1 x r x y x u x
3 1 3 x g x y x
5 x t s t x s r

2、将c.txt中的行替换为a.txt的1、3、5行

[root@PC3 test]# cat a.txt
e r e y e u e
e e g e 3 h r
1 3 e g e y e
e s e e e e e
e t s t e s r
d g e s w t e
[root@PC3 test]# cat c.txt
1 x r x y x u x
3 1 3 x g x y x
5 x t s t x s r
[root@PC3 test]# cp a.txt a.txt.bak
[root@PC3 test]# cat c.txt|while read i; do a=$(echo $i|cut -d " " -f 1); b=$(echo $i|cut -d " " -f 2-); sed -i "$a c $b" a.txt ; done
[root@PC3 test]# cat a.txt
x r x y x u x
e e g e 3 h r
1 3 x g x y x
e s e e e e e
x t s t x s r
d g e s w t e
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15054979.html