From an answer to a Stack Overflow query ...
This perl
:
$ perl -MDDP -lane 'BEGIN{ local @ARGV=shift; @tmp = map {[split]} <>; p @tmp }
$name = s/>//r if /^>/ ; push @out , [ $name, length $_ ] if !/^>/ ;
END{ p @out }' output.txt datafile2.txt
Produces ....
[
[0] [
[0] "one",
[1] 5
],
[1] [
[0] "two",
[1] 3
],
[2] [
[0] "three",
[1] 1
]
]
[
[0] [
[0] "one",
[1] 7
],
[1] [
[0] "two",
[1] 7
],
[2] [
[0] "three",
[1] 2
]
]