简体   繁体   中英

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

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

Remove the | bc | 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 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 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM