Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to invoke a command for every line of the standard input, much like xargs, but the line should be passed as standard input, not as a command-line argument:

cat some-file.txt | <magic> | wc -c
  • this should print the number of characters in each line of the file.

What are my options?

(The actual use case is processing a tail -f/grep combination with jq which seems to only start processing when it reaches EOF.)

share|improve this question

How about a plain loop

while IFS= read -r line ;
   printf "%s" "$line" | wc -c
done < some-file.txt
share|improve this answer

A while-read loop is the most clear. If you want to use xargs to do something for each line, you may end up with a monstrosity like this:

printf "%s\n" "foo bar" one " 1 2 3" | 
xargs -d '\n' -n 1 -I LINE bash -c 'wc -c <<< "LINE"'

Pretty expensive since you have to spawn a bash process for each line.

share|improve this answer
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

share|improve this answer

Your Answer


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.