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
2、将3-5列中的e替换为x
[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 [root@PC3 test]# awk '{for(i = 1; i <= NF; i++) if(i >= 3 && i <= 5 && $i == "e") {$i = "x"} {print $0}}' a.txt e r x y x u e e e g x 3 h r 1 3 x g x y e e s x x x e e
3、将1、3、5列中的e替换为x
[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 b.txt 1 3 5 [root@PC3 test]# cp a.txt a.txt.bak [root@PC3 test]# for i in $(cat b.txt ); do awk -v a=$i '{for(i = 1; i <= NF; i++) if(i == a && $i == "e") {$i = "x"} {print $0}}' a.txt > a && mv a a.txt; done [root@PC3 test]# cat a.txt x r x y x u e x e g e 3 h r 1 3 x g x y e x s x e x e e x t s t x s r d g x s w t e
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。