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

2、将每行的第3个e即其后的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]# max=$(awk -F e '{print NF-1}' a.txt | sort -rn | head -n 1)
[root@PC3 test]# echo $max
6
[root@PC3 test]# cp a.txt a.txt.bak
[root@PC3 test]# for i in `seq $max`; do sed -i 's/e/x/3' a.txt ; done
[root@PC3 test]# cat a.txt
e r e y x u x
e e g x 3 h r
1 3 e g e y x
e s e x x x x
原文地址:https://www.cnblogs.com/liujiaxin2018/p/15054337.html