Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Given input:


the desired output is

tree 4
branch 6

What is a *nix available command that does this?

share|improve this question
up vote 2 down vote accepted

A simple shell script which does this:

while read line; do
    printf -- '%s %d\n' "${line}" "${#line}";
done < "${1}"

save the script as e.g. lc, make it executable and run it on file names as

lc myfile

Or with awk:

awk '{print $0, length()}' myfile

Or with Perl:

perl -nle 'print $_." ".length($_)' myfile
share|improve this answer
Thank you for the variety of solutions. – e a005 May 11 '12 at 15:14

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.