将零添加到列表 [英] Append zeros to list

查看:74
本文介绍了将零添加到列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下输入文件:

a,10,12,13
b,20,22
c,30
d,33

,并希望在每个行包含三个数字之前附加零,因此应为输出:

and would like to append zeros until each line has three numbers, so this should be the output:

a,10,12,13
b,20,22,0
c,30,0,0
d,33,0,0

sed中,我可以使用以下两个命令:

In sed I could do use these two commands:

sed 's/\([a-z],[0-9]\+$\)/\1,0,0/g'
sed 's/\([a-z],[0-9]\+,[0-9]\+$\)/\1,0/g'

我的正则表达式知识仅限于sed,我想知道如何使用awkperl来实现这一点.

My regex knowledge is limited to sed and I would like to know how to achieve this with awk or perl.

推荐答案

$ awk 'BEGIN{FS=OFS=","} {for (i=2;i<=4;i++) $i+=0} 1' file
a,10,12,13
b,20,22,0
c,30,0,0
d,33,0,0

这篇关于将零添加到列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆