文本aaaaabbbbbcccccdddd eeeeefffffkkkkkvvvv nnnnnggggg 希望得到的结果如下:
aaaaa|bbbbb|ccccc|dddd eeeee|fffff|kkkkk|vvvv nnnnn|ggggg 匹配要求:希望每5个字符之间插入一个"|",如果该行的字符数量正好是5的倍数,行末就不插入"|"了。
解题如下:
[[email protected] zuoye2]# cat file
aaaaabbbbbcccccdddd
eeeeefffffkkkkkvvvv
nnnnngggg
[[email protected] zuoye2]# vi file
[[email protected] zuoye2]# awk ‘BEGIN{FS=OFS=""}{for(i=1;i<=NF;i++){if(i%5==0&&i!=NF){printf $i"|"}else{printf $i}}print ""}‘ file
aaaaa|bbbbb|ccccc|dddd
eeeee|fffff|kkkkk|vvvv
nnnnn|ggggg
[[email protected] zuoye2]#
时间: 2024-12-24 14:06:52