One could quickly brew their own solution in Python:
#!/usr/bin/env python
import sys
count = 0
while True:
byte = sys.stdin.read(1)
if not byte:
break
count = count + 1
print(count)
Works as so:
$ echo "Hi" | ./count_stdin_bytes.py
3
$ echo "Hello" | ./count_stdin_bytes.py
6
$ dd if=/dev/zero bs=1 count=1024 2>/dev/null | ./count_stdin_bytes.py
1024
Since in your particular case you're dealing with text data ( judging from the fact that you pipe from grep
), you could also make use of bash
's read
. Something like this:
$ echo "Hello" | { while read -n 1 char; do ((count++)) ;done ; echo $count; }
6
wc -c
becausedu -h
returns4.0 K
if it is any smaller than 4,0k as it reads in blocks – Stan Strum Feb 27 '18 at 16:18| wc -c | sed 's/$/\/1024\/1024/' | bc
. This appends/1024/1024
to the output and runs a calculator on the resulting string. – phil294 Dec 05 '19 at 10:30