系统相关
首页 > 系统相关> > linux系统中如何将每行特定数目字符后的字符替换为指定字符

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

 

标签:字符,max,每行,PC3,cat,linux,test,txt,root
来源: https://www.cnblogs.com/liujiaxin2018/p/15054337.html