How to pad zeroes based on input string?

Hello everyone,

I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine.

But I want to compare two values which will come at 4 precision places.

ex:
file1
Date,Count,Checksum
01/31/2014,3702,-170552450514.86
 
command:
cat $trigger_file  | sed '1d' | tr -s ' ' | cut -d, -f3 > trigger_file.txt
-170552450514.86
 
file2
 
sed '1d' ${data_file} | cut -d',' -f7 | awk '{s+=$1}END{ printf("%.4f\n",s)}' > data_file.txt
-170552450514.8600
 

My requiremnet is

file1

The source file will come at 4 precision values (file1).if the value is 123.42 it will not display as 123.4200 and 123.4 will not be 123.4000 and 0 will not come in as 0.0000

I Need to display it padding with 0's at the end until it becomes 4 precision points .I have searched in this forum for answers before posting, my requirement is based on input,

I need to pad 0's so that i can compare it with file2
ex:
0 should be 0.0000
1.2 should be 1.2000
-5.023 should be -5.0230
6.2345 should be 6.5345

Could you please help me

awk -F"," ' NR > 1 { printf("%.04f",$3) } ' $trigger_file  

What's wrong with applying your (adapted) awk snippet to your trigger file in lieu of that pipe organ? Other than you need to add 0.3 to achieve

Hi Andu23,

Thanks for the reply..

I am not able to display any result with the command.

 
cat test.txt
01/31/2014,3702,-170552450514.86
 
cib-sokay2{}:awk -F"," ' NR > 1 { printf("%.04f",$3) } ' test.txt
cib-sokay2{}:
 

Could you please help

If you don't have header in your input file then remove NR > 1 from awk

1 Like

Thanks Anbu ..its working fine..i am facing a starnge issue now:)

command given by you works fine if there is no control m appended at the end of the line or else it will display 0.0000 ....is there any way i can remove controlm from the file and then use the awk command...i tried with sed but it is not working

sed 's/^M//g' abc.csv > def.csv
mv def.csv abc.csv
 
awk -F"," ' NR > 1 { printf("%.04f",$3) } ' abc.csv
0.0000
 
vi abc.csv
As Of Date,Count,Checksum^M
01/31/2014,3702,-170552450514.86^M
 
tr -d '\015' < abc.csv > def.csv
mv def.csv abc.csv
1 Like

Why not all-in-1:

awk -F"," ' NR > 1 { sub (/\r$/,"");printf ("%.04f",$3) } ' abc.csv

?