echo | grepis unwarranted.bashunderstands regular expressions ("$line" ~==~ regex), and can do simple substitutions:line=${line#[[:space:]]*\/\/}removes leading whitespaces , followed by the comment, just what we are after.Replacing the file while reading it looks suspiciously. I recommend to have a destination file, and copy valid lines (and don't copy undesired ones). A perk benefit is that forking
sedis not needed anymore.
A side note: the script makes a false positive in cases like
// Notice that
// some_valid_c_code;
// doesn't work, because etc
The part of the comment would be recognized as a dead code, and the output will be
// Notice that
// doesn't work, because etc