perl - Character count (length) within specific column -


is there one-line method obtain character length strings held within specific column of tab-delimited .txt file , append these counts onto final column (number of columns may variable)?

sample data:

1   aa 2   bbb 3   ccccc 4   ee 5   ddd 6   aaa 7   fffff 8   aa 9   bbb 10  nnn 

to counts, have attempted use:

perl -lane 'print length $f[2]' in > out  perl -f, -mopen=:locale -lane 'print length $f[2]' in > out 

however, results empty.

i have tried:

perl -lane '$_.=$f[2]; print length $_' 

but this, realise, prints number of characters entire line rather specific column.

i not sure how append final column.

desired output (when counting column 2):

1   aa      2    2   bbb     3 3   ccccc   5 4   ee      2 5   ddd     3 6   aaa     3 7   fffff   5 8   aa      2 9   bbb     3 10  nnn     3 

it seems close. perl array indices start @ zero, how using length of $f[1]? need sort of separator

perl -lape '$_ .= "\t". length($f[1])' input 

output

1   aa  2 2   bbb 3 3   ccccc       5 4   ee  2 5   ddd 3 6   aaa 3 7   fffff       5 8   aa  2 9   bbb 3 10  nnn 3 

if want output exactly show, need use printf this

perl -lane 'printf qq{%-4d%-8s%d\n}, @f, length($f[1])' input 

output

1   aa      2 2   bbb     3 3   ccccc   5 4   ee      2 5   ddd     3 6   aaa     3 7   fffff   5 8   aa      2 9   bbb     3 10  nnn     3 

Comments