In a file containing lines like this one:
# lorem ipsum blah variable
I would like to remove # (comment) character in the same line that contains a specific string, in place. Is sed good for this?
I'm struggling to get this conditional working. I have a "clumsy" way of doing this; I can find the matching line number with awk or sed and then use this number in a separate sed command, but I believe that this can be done in a much better way.
's/#//g'will remove all the#characters in the line. If that’s not what you want, remove theg(which stands for “global”). (2) To edit a file in place (as asked for in the question), usesed -i. – G-Man Says 'Reinstate Monica' Sep 12 '14 at 21:54sed -i '/ipsum/s/#[[:space:]]*//', to get rid of any spaces and tabs immediately following the#. (4) You might also want to consider verifying that the#is the first non-blank character in the line. The current command would delete the#from the lineprompt "Enter # of ipsums:". – G-Man Says 'Reinstate Monica' Sep 12 '14 at 21:55sed -i '/ipsum/s/^#[[:space:]]*//'?! (^signifies start of line,$for end of line) - at least in gnu sed... – Jeremy Davis Oct 19 '17 at 06:02^, but that would be wrong. I often comment out indented code by putting the#immediately before the code, so the#is indented. I doubt that I’m the only person who does that. – G-Man Says 'Reinstate Monica' Oct 21 '17 at 03:50^&#, so something more like this:/ipsum/{/^[[:space:]]*#/s/#[[:space:]]*//}. Although even then, depending on where the#is, it may still cause issues (e.g. in languages that use whitespace indentation/separation). – Jeremy Davis Oct 22 '17 at 00:04