I'm trying to calculate the average entropy of files contained in a folder using:
{ echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | wc -l; } | tr -d '\n' | bc -l
entropy
being an executable which calculates the Shannon entropy of a file, giving outputs of the form:
$ entropy foo
foo: 5.13232
The aforementioned command errors out with:
(standard_in) 1: syntax error
However, the generated output seems to have no problems:
$ { echo '('; find . -type f -exec entropy {} \; | grep -Eo '[0-9.]+$' | sed -r 's/$/+/g'; echo '0)/'; find . -type f | wc -l; } | tr -d '\n'
(5.13232+2.479+1.4311+0)/3
And this works too:
$ echo '(2.1+2.1)/2' | bc -l
2.1
What is wrong with the mentioned command?
awk
? Would be substantially easier. – Bernhard yesterdaybc
command: compareprintf '(5.13232+2.479+1.4311+0)/3' | bc -l
withecho '(5.13232+2.479+1.4311+0)/3' | bc -l
. (yourtr -d '\n'
command removes the trailing newline thatbc
needs). – gniourf_gniourf yesterday{ cat; echo; }
between thetr
and thebc
:tr -d '\n' | { cat; echo; } | bc -l
or to replace thetr -d '\n'
part with:{ tr -d '\n'; echo; }
– gniourf_gniourf yesterdaypaste -sd'\0' -
instead oftr -d '\n'
to preserve the last newline character. (see alsopaste -sd+ -
to join lines with+
). – Stéphane Chazelas yesterday