Saturday, May 17, 2008

Add a decimal point


ints.txt is a file containing some integer values.


$ cat ints.txt
234
2
67
9812
12890


I had to convert all the values to 2 decimal points to right.

i.e. required output:

2.34
.02
.67
98.12
128.90


Sed solution:

$ sed 's/..$/.&/;t;s/^.$/.0&/' ints.txt


Can be done just by dividing by 100 using awk like this


$ awk '{print $0/100}' ints.txt
2.34
0.02
0.67
98.12
128.9

1 comment:

Karan Bohra said...

while read num; do dc -e "2k$num 100/p"; done < ints.txt

© Jadu Saikia www.UNIXCL.com