I need to change the
characters that are between (but not including) "StringA" and "StringB" and
also all of the characters between (but not including) the first set of
parentheses after "StringC" (there are an unknown amount of characters
between "StringC" and the parentheses)
OK, there's a lot going on here. First of all, sed is a very line-oriented tool. If you're trying to do something complicated like this on text that isn't strictly delimited by lines, you're better off with something else, like Perl. This little Perl script should do the trick; if you save it as "
hyphenate.pl", then you can run it with "perl hyphenate.pl <oldfile >newfile", or as "perl -i file" to modify the file in place.
# Unset the delimiter so the read will slurp the whole file instead of just the first line
undef $/;
# read in the file
$_ = <>;
# split out the first part we want to change
my ($before, $changeme, $after) = /\A(.*StringA)(.*)(StringB.*)\Z/sm;
# change it
$changeme =~ s/[^a-zA-Z0-9:]/-/g;
# put it all back together
$_ = $before . $changeme . $after;
# split out the next part we want to change
($before, $changeme, $after) = /\A(.*StringC[^\(]*\()([^\)]*)(\).*)\Z/sm;
# change it
$changeme =~ s/[^a-zA-Z0-9:]/-/g;
# and put it all back together
$_ = $before . $changeme . $after;
# then print it back out
print;
--