Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

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.