Reputation: 473
I am dealing with a file which has the following form:
"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441
Sometimes, there are values with no decimals (e.g. 441 instead of 441.0) and I need the decimals to be there. How do I write a script such that all integers are added .0 so that they become floats?
Upvotes: 2
Views: 97
Reputation: 4950
Save this awk
script as awk.src:
BEGIN {
FS=";"
}
#
## MAIN Block
#
{
printf $1; printf FS;
for (i=2;i<=NF;i++) {
if ($i !~ "\\.") {
printf "%.1f", $i;
}
else { printf $i; }
if (i!=NF) {
printf FS;
}
else { printf "\n"; }
}
}
Try it:
$ awk -f awk.src < sample.txt
"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441.0
Upvotes: 2
Reputation: 11216
With sed
sed 's/\(;[^\.]*\)\(;\|$\)/\1.00\2/g' file
just a simple replacement regex.
"1999-01-04";1391.12;3034.53;66.515625;86.2;441.39
"1999-01-05";1404.86;3072.41;66.3125;86.17;440.63
"1999-01-06";1435.12;3156.59;66.4375;86.32;441.00
Upvotes: 5