how to count string length?

Hi, Gurus,

I have a requirement which need count string length.
eg.

abc
bcde
defge

want to get following:

abc    3
bcde   4 
defge  5

:wall:

thanks in advance!

awk '{ $(NF+1)=length($1) } 1' inputfile > outputfile

another way

while read line
 do 
   printf "$line ${#line} \n"
 done < infile
awk '$2=gsub(/./,"&")' infile
1 Like

Hi it works, But I don't understand :frowning:

1)I don't see the print
2) what does't men for the last "1"

can you explain to me

Thanks

Hi, yang lei, everything in awk has the form condition{action} . If the condition evaluates to 1 then the action is performed. If the condition is omitted then the default condition is 1, so the action is always performed. If the action is omitted then the default action is performed, which is {print $0} . In this case the condition is "1" so that evaluates to 1 and the action is omitted, therefore {print $0} is performed, which is "print the entire record".

Using expr.... :slight_smile:

while read line
do
 echo "$line" "`expr "$line" : '.*'`"
done < inputfile