Your first exception is not trivial to implement, but maybe you can rephrase it as part of the substitution? So to replace "foo" with "bar" except on those lines, I'd go with
sed '/parameter\|,/!s/^\([^!]*\)foo/\1bar/'
(Some sed
dialects will want more or fewer backslashes.)
/parameter\|,/!
will only select lines which do not match this regular expression.
s/^\([^!]*\)foo/\1bar/
will substitute "foo" with "bar" provided that "foo" is not proceded by an exclamation mark. The leading text is captured into a back reference \1
and basically replaced with itself.
The case insensitive matching requires an ugly regex in sed
-- [Pp][Aa][Rr][Aa][Mm][Ee][Tt][Ee][Rr]
. For anything more complex, maybe switch to Awk or Perl, which provide better facilities for complex matching as well as much more readable code.