Counting rows line by line from a specific column using Awk

Dear UNIX community,

    I would like to to count characters from a specific row and have them displayed line-by-line.

I have a file called testAwk2.csv which contain the following data:

rabbit penguin goat 
giraffe emu ostrich 

I would like to count in the middle row individually line by line and append the result to the end of each line so the final result appears as follows:

rabbit penguin goat 7
 giraffe emu ostrich 3

I have tried the following:

awk '{print $2, wc -m}' testAwk2.csv

Result:

penguin 0
emu 0
awk '{print $2 | "wc -m"}' testAwk2.csv

Result:

12

I thought the above code would produce something like:

penguin 7
emu 3

But it didn't:wall:

As you can see from the above code that I have not attempted to append a character count to the end of the line and instead have tried to concentrate in displaying the count for the individual rows.

Any suggestions or tips would be received gladly.

Many thanks,

vnayak

awk '{print $0, length($2)}'  testAwk2.csv 
1 Like
awk '$0=$0 FS length($2)' testAwk2.csv
1 Like

Hi,

Many thanks for your code. That helped a lot. I was literally barking up the wrong tree until I tried out your code.

Thanks once again. I really appreciated the quick response!

Vnayak

Same thing in perl..

 
perl -lane 'print $_ ." ". length $F[1]'