Categorically speaking, when I run into complex tasks like these, I (now...) instinctively turn to the Perl programming-language.
My essential point, though, is that you should turn to
some "programming language." Perl, Python, PHP, and Ruby are all probably within easy reach. You can write a command-line script in any of these, just as easily as you can any shell-script, and
no one will know or care exactly how you did it.
"Shell scripting" is a great resource, but I think it's best "in small doses." It is only
one of the guns in your holster, and really it's the least one.
The Perl folks like to say "TMTOWTDI = There's More Than One Way To Do It," and while they sometimes carry that thought to an extreme
there
is a lot of truth to it. Linux/Unix gives you an uncommonly-rich programming environment
vis-a-vis "You Know Who." It is well worth your time to explore the full extent of just what has been exciting computer-programmers since the 1970's...