AnnaSchumann
AnnaSchumann

Reputation: 1271

Character count (length) within specific column

Is there a one-line method to obtain character length for strings held within a specific column of a tab-delimited .txt file and then append these counts onto the final column (number of columns may be variable)?

Sample Data:

1   AA
2   BBB
3   CCCCC
4   EE
5   DDD
6   AAA
7   FFFFF
8   AA
9   BBB
10  NNN

To get the counts, I have attempted to use:

perl -lane 'print length $F[2]' in > out

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

However, the results are empty.

I have also tried:

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

But this, as I now realise, prints the number of characters for the entire line rather than a specific column.

I am not sure how I would then append the 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

Upvotes: 1

Views: 102

Answers (1)

Borodin
Borodin

Reputation: 126722

It seems that you were close. Perl array indices start at zero, so how about using the length of $F[1]? You will also need some 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 you want the output exactly as you show, then you will need to use printf like 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

Upvotes: 3

Related Questions