Input file:
$ cat ids.txt
00009
01902
34390
00190
00001
Required: Remove the leading zero's from each of the lines in the above file.
Using sed:
$ sed 's/^[0]*//' ids.txt
Output:
9
1902
34390
190
1
So in vi ex mode, the command for the same will be:
:1,$ s/^[0]*//
Other alternatives:
$ awk '{printf "%d\n",$0}' ids.txt
$ awk '{print $1 + 0}' ids.txt
$ cat ids.txt | bc
Using bash parameter substitution:
$ shopt | grep extglob
extglob off
# Set this option on
$ shopt -s extglob
$ i=000100
$ echo ${i##+(0)}
100
extglob shell option in bash:
If set, the extended pattern matching features are enabled
Now if you are thinking how we can add zero's at beginning of a bash variable, here is the way using printf:
$ printf "%010d\n" 00005
0000000005
You might look at this post which shows how we can make some numbers equal width by padding the number with leading zeroes.
Some related posts:
Replace leading zero's with blank using sed
Remove white space in vi editor


