File size distribution: Difference between revisions

Add POSIX sh version
(Add Tcl version)
(Add POSIX sh version)
Line 1,594:
puts "[? {$key == -1} 0 {1e$key}]\t[dict get $hist $key]"
}</lang>
{{out}}
<pre>0 1
1e1 339
1e2 3142
1e3 2015
1e4 150
1e5 29
1e6 13
1e7 3</pre>
 
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
Completely POSIX comformant, too.
<lang sh>#!/bin/sh
set -eu
 
# Use a subshell to remove the last "total" line per each ARG_MAX
# Truncate outside bc because fumbling with scale is a pain
find -- "${1:-.}" -type f -exec sh -c '
wc -c -- "$@" | awk '\''{if (NR > 1) print buf; buf = $1}'\''' argv0 {} + | \
{
echo 'define f(x) {if (!x) return -1; return l(x)/l(10)}'
sed 's/^/f(/; s/$/)/'
} | \
bc -l | \
cut -d. -f1 | \
sort -n | \
uniq -c | \
awk -vOFS='\t' '{print ($2 == - 1 ? 0 : "1e" $2), $1}'</lang>
{{out}}
<pre>0 1
Anonymous user