cat file.txt | while IFS= read -r i; do echo -n "$i" | wc -c; done ## or (better): while IFS= read -r i; do echo -n "$i" | wc -c; done < file.txt
However, this will just print the number of characters on a line, one line at a time. If you want something more readable, you might want one of the following:
## Prints the contents of each line underneath the character count: while IFS= read -r i; do echo -n "$i" | wc -c; echo "$i"; done < file.txt ## Prints the line number alongside the character count: n=0; while IFS= read -r i; do n=$((n+1)); echo -n "line number $n : "; echo -n "$i" | wc -c; done < file.txt
For greater portability you could use printf '%s' "$i" instead of all the echo -ns
--unbufferedflag these days but didn't back then).jqat the end of the question, as it implies that the data is in fact not line-based at all but structured. Any answer using line-based tools would therefore be inadequate in various ways. Also note that although the question is almost ten years old, answers may be added that solves it using up-to-date tools.awk '{ print length }'as the second and final stage of the pipeline solve the particular problem here? Or were you stuck having to callwc -cspecifically? The question is whether the issue is philosophical (and the commands shown are examples) or actual (and you actually want to count the length of each line in a particular way).wcwas just a random example.