Search code examples
kshprecisiondigits

KSH remove digits from number precision


I am trying to compare 2 log files in a ksh script that look like

log1:

10100 951 5 20150318 20150430 
10101 11950 0 20150323 20150630 
10102 285933 1 20150128 20150430 
10041 57007 3.53 20150128 20150430

log2

10100 951 5.0000 20150318 20150430
10101 11950 0.0000 20150323 20150630
10102 285933 1.0000 20150128 20150430
10041 57007 3.5300 20150128 20150430

Log1 on column 3 has maximum 2 digits after the . (eg: 3.53)

Log2 on colum 3 always has 4 digits after the . (eg: 0.0000 or 3.5300)

How could I add some digits after . for the first log or remove the digits in log2 in order to be able to compare them line for line?

My script is written in ksh.


Solution

  • You should format the value with printf:

    cat log1 | while read col1 col2 col3 col4 col5; do
       printf "%d %d %.4f %d %d\n" ${col1} ${col2} ${col3} ${col4} ${col5}
    done > log1.converted
    

    Above code reads easy, but makes an unnecessary call to cat ("UUOC").
    The better way to write this is

    while read col1 col2 col3 col4 col5; do
        printf "%d %d %.4f %d %d\n" ${col1} ${col2} ${col3} ${col4} ${col5}
    done < log1 > log1.converted