Super User is a question and answer site for computer enthusiasts and power users. 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

Given input:

tree
branch

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:

#!/bin/sh
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .