Filling out a text

Hello,

I have a problem with filling out a text. I have different lenghts in a file and would like to see that all the lines becomes the same length by putting a zero in front off the line.

Please advice.

Old File:
----------
5432
233
3455
4432

New File:
-----------
5432
0233
3455
4432

awk 'BEGIN {m=0} {a[NR]=$0;if (m<length($0)){ m=length($0)}} END { for (x in a) { if (length(a[x])<m) {  for (i=length(a[x]);i<m;i++) { printf "0" }  } print a[x] }} ' file 

No need for that for loop to push zeros in front - printf is smart enough:

$ nawk '{a[NR]=$0;m<length($0)&&m=length($0)}END{for(i in a){printf "%0"m"d\n", a}}' of
0233
3455
4432
5432

if he needs only want it as 4 field then it will help

awk '{printf "%04d\n" ,$0}' filename

So we are doing max(different).

if you have Python

# more file
5432
233
3455
44322332423

# python -c 'data=open("file").read().split();print "\n".join([ i.zfill(max(map(len,data))) for i in data ])'
00000005432
00000000233
00000003455
44322332423


awk '{
_[NR]=$0
len=length($0)
if (len>n)
	n=len
}
END{
	for(i=1;i<=NR;i++)
		printf("%"n"s\n",_)
}' filename | sed 's/ /0/g'