Here is a straightforward way to do this using awk
echo "Buddies.forlife.com" | awk -F '.' '{ printf( "|0x%x|%s|0x%x|%s|0x%x|%s\n", length($1),$1, length($2),$2, length ($3), $3 )}' |0x7|Buddies|0x7|forlife|0x3|com
The awk command is setting the field separator -F '.'
to a period, so each segment is considered it's own field.
We can then refer to those fields by $1, $2 and $3
, use the builtin function length
to get the character count for each field, and use a printf to print out the results (%x
for hex numbers, %s
for the original strings, and output field separation using the '|' symbol, in the format you had specified.
The 0x
prefix is typically used to indicate hex numbers, but can be changed to something else (or nothing) as needed.