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

 

001、

root@PC1:/home/test3# ls
a.txt
root@PC1:/home/test3# 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@PC1:/home/test3# cp a.txt a.txt_bak
root@PC1:/home/test3# max=$(awk -F "e" '{print NF - 1}' a.txt | sort -rn | head -n 1)
root@PC1:/home/test3# echo $max
6
root@PC1:/home/test3# let loop=$max-2
root@PC1:/home/test3# echo $loop
4
root@PC1:/home/test3# for i in $(seq $loop); do sed -i 's/e/x/3' a.txt; done  ## 每一行最多宝贵2个e
root@PC1:/home/test3# ls
a.txt  a.txt_bak
root@PC1:/home/test3# 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

 

posted @ 2022-07-22 23:24  小鲨鱼2018  阅读(195)  评论(0编辑  收藏  举报