0

I have the following lines in a file on WSL

// title
// (
//     "line"
// )

// SomeOtherTitle
// (
//     "otherLine"
// )

I would like to use the sed command to uncomment the lines.

// title
// (
//     "line"
// )

And leave the rest commented.

I am going to use this in a bash script to uncomment the information several files. How can this be achieved?

I have tried:

sed 's#^// [ \t]$title#title#g' file
sed 's#^// [ \t]$(#(#g' file
sed 's#^// [ \t]$"line"#"line"#g' file # this one did not work for me
sed 's#^// [ \t]$)#)#g' file

Kind regards

4
  • What operating system are you using? We need to know what sed you have and what other tools might be available. Also, can there be other cases of // ( or // ) in the file which you do not want to uncomment or should every single line with a commented ( or ) as the first non-whitespace character be uncommented?
    – terdon
    Jan 13 at 16:28
  • I am using windows subsystem for linux with Ubuntu 20.04. Currently I only have one comment with that shape. It would be good if it was possible to find a match to title and uncomment the next two lines. That would also work Jan 13 at 17:04
  • You mean the next three lines, right?
    – terdon
    Jan 13 at 17:57
  • yes, the three lines Jan 13 at 18:41

1 Answer 1

0

First a minor point: using g is useless if you are anchoring the regex with ^ since there can only ever be one match. Now, for the main issue, none of the commands you show would ever match anything since they all have a $ in the left hand side. The $ means "end of the line" so nothing will be matched after that. In addition, you're using [ \t] which means one of space or tab. What you want is "0 or more" (specifically, you want one or more but 0 or more should work here), so you wanted [ \t]*. These should do what you want:

sed 's#^//[ \t]title#title#' file.c 
sed 's#^//[ \t](#(#' file.c 
sed 's#^//[ \t]*"line"#"line"#' file.c
sed 's#^//[ \t])#)#' file.c 

But this isn't a good idea since that would replace all cases of commented ( or ) if they are the first non-space or tab character on the line.

However, if you can be sure that you always want to uncomment the line // title and the three lines after that, you could do:

perl -pe 'if(m#^\s*//\s*title#){ $k=1 } 
          $k=0 if $k==3;
          s#^\s*//## if $k' file.c

On your input file, that produces:

$ perl -pe 'if(m#^\s*//\s*title#){ $k=1 } 
          $k=0 if $k==3;
          s#^\s*//## if $k' file.c
 title
 (
     "line"
 )

 SomeOtherTitle
 (
     "otherLine"
 )
2
  • can perl edit the file, like sed -i ? Jan 13 at 18:48
  • @mysticsnake yes, in fact I think sed might have taken the -i from perl, but I'm not sure. In any case, just use perl -i.bak -pe '... and it will work like sed, creating a backup copy of the original file, renamed to foo.bak.
    – terdon
    Jan 13 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.