Home > OS >  What percentage of connections resulted in errors.... to 3 decimal places
What percentage of connections resulted in errors.... to 3 decimal places

Time:07-16

Each access to the web server results in either an access log entry or an error log entry. So that the total entries in access log total entries in error log = total access attempts. What percentage of connections resulted in errors.... to 3 decimal places.

I tried piping the 2 files to do a percentage error, but I get the following error

 echo 'scale=3;' 'cat error.log | wc -l' / '(' 'cat access.log | wc -l'   'cat error.log | wc -l' ') * 100.0' | bc
(standard_in) 1: syntax error
(standard_in) 1: illegal character: |
(standard_in) 1: syntax error
(standard_in) 1: illegal character: |
(standard_in) 1: syntax error
(standard_in) 1: illegal character: |
(standard_in) 1: syntax error

CodePudding user response:

Remove the | bc (at the end) and run the command; you're generating one long line of text and then feeding it to bc ...

scale=3; cat error.log | wc -l / ( cat access.log | wc -l   cat error.log | wc -l ) * 100.0

... but this long line of text is just gibberish as far as bc is concerned.

One issue, the cat ... | wc -l is just a string of characters; these strings do not actually call cat nor wc.

Taking this one step at a time ...

$ err_cnt=$(cat error.log | wc -l)
$ acc_cnt=$(cat access.log | wc -l)
$ echo "scale=3; ${err_cnt} / ( ${err_cnt}   ${acc_cnt} ) * 100" | bc

Assuming err_cnt=5 and acc_cnt=91 this generates:

5.200

One issue here is that the scale is applied at each step in the calculation which causes the loss of accuracy.

Consider a small modification:

$ echo "scale=3; ( ${err_cnt} * 100.0 ) / ( ${err_cnt}   ${acc_cnt} )" | bc
5.208

While it would be possible to eliminate the 2 variables (err_cnt and acc_cnt) by embedding the cut | wc operations within the echo ...

$ echo "scale=3; ( $(cat error.log | wc -l) * 100.0) / ( $(cat error.log | wc -l)   $(cat access.log | wc -l) )" | bc
5.208

... this gets a bit cumbersome while also requiring 2 scans of the error.log file.

  • Related