Linux教程

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

本文主要是介绍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

 

这篇关于linux系统中如何将每行特定数目字符后的字符替换为指定字符的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!