Algumas opções:
tr -cd 'tr -cd '%pre%' | wc -c
tr '\n%pre%' '%pre%\n' | wc -l # Generic approach for processing NUL-terminated
# records with line-based utilities (that support
# NUL characters in their lines like GNU ones).
grep -cz '^' # GNU grep
sed -nz '$=' # recent GNU sed, no output for empty input
awk -vRS='%pre%' 'END{print NR}' # not all awk implementations
' | wc -c
tr '\n%pre%' '%pre%\n' | wc -l # Generic approach for processing NUL-terminated
# records with line-based utilities (that support
# NUL characters in their lines like GNU ones).
grep -cz '^' # GNU grep
sed -nz '$=' # recent GNU sed, no output for empty input
awk -vRS='%pre%' 'END{print NR}' # not all awk implementations
Observe que, para uma entrada que contém dados após o último caractere NUL (ou entrada não vazia sem caracteres NUL), as abordagens tr
sempre contarão o número de caracteres NUL, mas o awk
/ sed
/ grep
abordagens contará um registro extra para esses bytes extras.