Usually the recipe for writing good regular expressions is to phrase the problem correctly - most of the times this alone is providing the solution.
In your case you were *almost* there already, so this is simple:
First we filter out all the lines starting with "#". This is done by a special regexp device: "^" if used at the beginning of a regexp, means "start of line". That is: "^#" doesn't mean a caret-char followed by a octothorpe, but an octothorpe as the first char of a line. Here is the script with some sample text, everything filtered out is marked blue:
sed '/^#/d' file > file.changed
this line goes through
# this line is blocked
this line goes through even if it has a # in it
Now for the next problem: match a line with an exact content and print it (to a file). Your problem with the child directories could be stated as "match a line with a content and no additional content". We achieve this by using a similar device as above: the "$" at the end of a regexp means "end of line". That is: "x$" means not "x followed by a dollar sign", but "x as the last character of a line".
By the way, as we are just searching for specific lines and ignore all the others we could simply skip the filtering out of the lines starting with an octothorpe ("#"), as we won't find them anyways. we can simply turn off any output of sed (the -n option) and only explicitly print the found lines. I let the filter for the commentary lines in there, but it is redundant.
Here it is with a sample text, i marked blue what is printed out:
sed -n '/^#/d;/^this is my text to find$/p' file > file.changed
# this line is blocked by rule 1
this is my text to find but with additional text
this is my text to find
now for the last part, the adding of the additional parts: we simply change the rule 2 which finds and prints the text to a substitution. We use here the sed-capability to provide the matched part of the text in the output. The "&" in the substitution contains what we have really matched in the search expression:
sed -n '/^#/d;s/^this is my text to find$/& with added text/p' file > file.changed
# this line is blocked by rule 1
this is my text to find but with additional text
this is my text to find
The content of file.changed should be a single line "this is my text to find with added text".
We get back to your problem again: in your text there are slash-characters and as "/" is a part of the sed-syntax too you will have to "escape" it by putting a "\" in front of it: to match "/usr/bin" use the expression "\/usr\/bin".
Furthermore, it is most of the times a good idea to clear any unnecessary whitespace from a line prior to matching it. Most of the times we do NOT want to get trailing or leading blanks, tabs, etc. in the way and "match" and "<tab><blank>match" are quite the same. So I would write it that way ("<spc>" is a literal space, "<tab>" is a TAB character):
sed -n 's/^[<tab><spc>]*//
s/[<tab><spc>]*$//
/^#*/d
s/^\/the\/directory\/to\/find$/&/hello\/every\/one/p' > file.changed
Here is a last tip: when you prepare regexps, test them against short texts Prepare the most difficult examples you can think of. Notice four kinds of lines and try to provide one in each category:
The ones that are matched and should be matched;
the ones that are matched but shouldn't be matched;
The ones that are not matched but should be matched;
the ones that are not matched and correctly so.
bakunin