Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
Are you willing to use awk? Would be substantially easier. –  Bernhard yesterday
2  
You're just missing a trailing endline for the bc command: compare printf '(5.13232+2.479+1.4311+0)/3' | bc -l with echo '(5.13232+2.479+1.4311+0)/3' | bc -l. (your tr -d '\n' command removes the trailing newline that bc needs). –  gniourf_gniourf yesterday
3  
An easy fix is to insert { cat; echo; } between the tr and the bc: tr -d '\n' | { cat; echo; } | bc -l or to replace the tr -d '\n' part with: { tr -d '\n'; echo; } –  gniourf_gniourf yesterday
2  
Use paste -sd'\0' - instead of tr -d '\n' to preserve the last newline character. (see also paste -sd+ - to join lines with +). –  Stéphane Chazelas yesterday
add comment

2 Answers

up vote 7 down vote accepted

And this works too: echo '(2.1+2.1)/2' | bc -l

Ah, but did you try:

echo '(2.1+2.1)/2' | tr -d '\n' | bc -l
(standard_in) 1: syntax error

Using echo -n will accomplish the same thing -- there's no terminating newline, and that's your problem.

share|improve this answer
add comment

bc has some pretty particular syntax. dc is less choosy:

find . -type f -exec entropy \{\} + |
sed 's/.*://;N;N;s/\n[^:]*:/+/g;s/+//;s|$| 3/p|' |
dc

I think that does what you're trying to do, but I'm not totally certain. An output sample larger than a single line would help.

share|improve this answer
2  
You could use dc to do all the work too: { find . -type f -exec entropy \{\} | sed 's/.*://' ; echo ' 10k[+]sa[z2!>az2!>b]sbzsclbxlc/p'; } | dc. The ugly 10k[+]sa[z2!>az2!>b]sbzsclbxlc/p is a bunch of junk that tells dc to compute the average of the numbers left on the stack (with a scale of 10) :D. –  gniourf_gniourf yesterday
    
@gniourf_gniourf - that's better than me, man. Make it an answer and I'll delete mine. –  mikeserv yesterday
    
No, yours is great! I can't really advocate this dc junk anyways—it's only good to be left in this comment area. –  gniourf_gniourf yesterday
1  
@gniourf_gniourf - it's pretty crazy fast, though. I wanna learn how to use it better. Thanks for the study material... –  mikeserv yesterday
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.