This is similar to Using sed to replace beginning of line when match found but a different question so hence this thread.
I wish to uncommented out commented out code. More specially, all cases where the variable myVar is commented out.
Example:
public class MyClass {
...
...
//myVar.setAge(200);
//myVar.setPlanet("mars");
}
to
public class MyClass {
...
...
myVar.setAge(200);
myVar.setPlanet("mars");
}
The regex:
^\\.*myVar.*$
Gets me everything I need.
The tricky part is getting the correct Sed. I try:
sed 's/(^\\)(.*myVar.*$)/\2/g' Build_DM_Digests_Batch.cls
On the following basis. Create two match groups. The first one is the commented out lines. And the second is the rest of line. Replace the entire line with just the second mateched group.
This gives error:
sed: -e expression #1, char 29: Unmatched ) or \)
Any tips?
Use
sed 's/^\( *\)\/\/\(.*myVar.*$\)/\1\2/' fileUse the
-ioption to save the changes in the filesed -i 's/^\( *\)\/\/\(.*myVar.*$\)/\1/' file:Explanation:
Here we capture the whitespace upto the
//and then everything after ifmyVaron the line and replace with\1\2.Your logic is almost there but a couple of things, firstly escaped all brackets and secondly you want
^( *)\/\/not^\\that is two escaped forwardslashes with the whitespace captured not two backslashes at the start of the line:If you don’t want to escape brackets you need to use the extended regexp flag of
sedwhich is-rforGNU sedonOSXit’s-Eso check withsed --help.Note: when you are matching the whole line (from
^to$) thegflag is redundant.