Don't underestimate the power of the dark side rovf
Problem here lies in to many nesting stuff and many pipes not the language.
for i in $( find | grep | sed | awk )
When you see shell code looking like bar code, something is fishy
Replace that with ls and awk magic, and stuff should happen much faster then now.
The OP should provide representative example of his input data and desired output to help further.
Also answer common question like operating system and shell.
Regards
Peasant.